• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python读写ini文件的方法

Python读写ini文件的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python ini文件,python读取ini文件,python解析ini文件,python读取ini,python 解析ini等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Python读写ini文件的方法。分享给大家供大家参考。具体如下:

比如有一个文件update.ini,里面有这些内容:

[ZIP]
EngineVersion=0
DATVersion=5127
FileName=dat-5127.zip
FilePath=/pub/antivirus/datfiles/4.x/
FileSize=13481555
Checksum=6037,021E
MD5=aaeb519d3f276b810d46642d782d8921

</div>

那就可以通过下面这些代码得到MD5的值,简单吧

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(open('update.ini'))
a = config.get("ZIP","MD5")
print a

</div>

写也很简单:

import ConfigParser
config = ConfigParser.ConfigParser()
# set a number of parameters
config.add_section("book")
config.set("book", "title", "the python standard library")
config.set("book", "author", "fredrik lundh")
config.add_section("ematter")
config.set("ematter", "pages", 250)
# write to file
config.write(open('1.ini', "w"))

</div>

修改也不难(添加内容):

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('1.ini')
a = config.add_section("md5")
config.set("md5", "value", "1234")
config.write(open('1.ini', "r+")) #可以把r+改成其他方式,看看结果:)

</div>

修改内容:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('1.ini')
config.set("md5", "value", "kingsoft") #这样md5就从1234变成kingsoft了
config.write(open('1.ini', "r+"))

</div>

删除部分就懒得写了,感兴趣的自己看文档:

remove_option( section, option)
Remove the specified option from the specified section. If the section does not exist, raise NoSectionError. If the option existed to be removed, return True; otherwise return False. New in version 1.6.
remove_section( section)
Remove the specified section from the configuration. If the section in fact existed, return True. Otherwise return False.

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python读写ini文件的方法
  • Python读写ini文件的方法
  • Python实现的ini文件操作类分享
  • Python实现的ini文件操作类分享

相关文章

  • python使用pil生成缩略图的方法
  • 深入解析Python中的集合类型操作符
  • python删除指定类型(或非指定)的文件实例详解
  • python通过pil模块获得图片exif信息的方法
  • 最大K个数问题的Python版解法总结
  • Python在Windows和在Linux下调用动态链接库的教程
  • python 链接和操作 memcache方法
  • 跟老齐学Python之使用Python查询更新数据库
  • python数据结构之图的实现方法
  • Python selenium 父子、兄弟、相邻节点定位方式详解

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 在IIS服务器上以CGI方式运行Python脚本的教程
    • Python创建模块及模块导入的方法
    • python多线程编程方式分析示例详解
    • 用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
    • Python设计模式之单例模式实例
    • 简单介绍Python中的len()函数的使用
    • python通过imaplib模块读取gmail里邮件的方法
    • python常见排序算法基础教程
    • python日志记录模块实例及改进
    • Python操作MongoDB数据库PyMongo库使用方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有