python文件操作之ini文件

来源:互联网 发布:需要人陪 王力宏 知乎 编辑:程序博客网 时间:2024/06/08 01:25

0x 01ini文件的格式

[student]   //SECTION是一个节的名称name = gumin   //section下面是键值对的组合sex = manbirthday = 1990[blonging]phone = iphone10scomputer = thinkpad

0x02  ConfigParser模块

ConfigParser 是用来读取配置文件的模块,下面我们通过代码示例来看一下它是如何使用的

#-*-coding:utf-8-*-import ConfigParser #引入ConfigParser模块cfg = Config.Parser.ConfigParser #初始化ConfigParsercfg.read("Config.ini") #导入测试的ini文件,参考0x00cfg.section() #输出section列表<img src="http://img.blog.csdn.net/20151025162245463" alt="" />cfg.items('student')  #输出student节下的所有键值对列表<img src="http://img.blog.csdn.net/20151025162348165" alt="" />cfg.options('student') #输出student节下所有key的值<img src="http://img.blog.csdn.net/20151025164205070" alt="" />cfg.get('student','name') #获取student节下的key值为name的value<img src="http://img.blog.csdn.net/20151025164354609" alt="" />cfg.set('student','name','gumin2') #修改name键对应的值,或者你新插入键值对,只要保证你的key是不重复的cfg.remove_option('student','sex') #删除sex键

0x03 Config文件管理类

待补充





0 0
原创粉丝点击