Python操作ini配置文件
2018-11-27 20:49
330 查看
很多项目都喜欢将一些配置信息放在一个类型为.ini文件里,可以方便文件的管理,比如如果后续相关配置信息需要需要,只需要更改对应的.ini文件即可,当然,配置信息也可以存储在很多其他类型的文件里,比如.json,.txt,.py等等,当然如果你高兴,你也一样存放在Excel文件或者.csv文件,只要能够获取到其中的数据即可,但是地球人比较熟悉的配置信息还都是放在.ini文件里
这里介绍两个常用的处理.ini文件的模块, configparser和configobj。
1.模块configparser
[code]# -*- coding: utf-8 -*- import configparser """ Python版本3.6 configparser已经在Python3.6默认安装了,如果没有,请使用你我他都知道的方式安装(pip install 名称) """ """在configparser中 配置文件[]的值称为section, 每组section中的键值对前面的称为option, 后面可以称之为value 注意点: 1.配置文件中的键值对可以为key=value,也可以为key:value 2.该模块在处理.ini文件中的注释的时候有点缺陷,如果注释和键值对在同一行,则该注释会被当做值的一部分,所以,注释一定要单独占一行 3.该模块在操作对应的section是不区分大小写,全当做小写处理 """ """创建一个config.ini文件,内容如下 [Section_A] a_key1 = 10 # 这是注释1 a_key2 = 20 # 这是注释2 [Section_b] host = 127.0.0.1 username = xxxxxx password = 123456 """ iniFileUrl = "config.ini" # 配置文件路径,也可以使用绝对路径 conf = configparser.ConfigParser() # 创建一个conf对象 conf.read(iniFileUrl, encoding='utf-8') # 读取ini文件 def read_ini_file(): # 获取ini文件中所有sections值 sections = conf.sections() # 返回一个列表 print("当前配置文件中的section内容有:", sections) # 获取每个section中所有的键option print(f"{sections[0]}中所有的键options:", conf.options(sections[0])) print(f"{sections[1]}中所有的键options:", conf.options(sections[1])) # 获取指定section中的所有键值对 print(f"{sections[0]}中所有的键值对为:", conf.items(sections[0])) print(f"{sections[1]}中所有的键值对为:", conf.items(sections[1])) # 获取指定section, option中的具体指value print(f"{sections[0]}中键a_key1的值为:", conf.get(sections[0], 'a_key1')) print(f"{sections[1]}中键host的值为:", conf.get(sections[1], 'host')) def write_ini_file(): sections = conf.sections() # 返回一个列表 conf.set(section=sections[0], option=conf.options(sections[0])[0], value="1000") # 若当前option已经存在,则修改 conf.set(section=sections[1], option="available", value="True") # 若当前option不存在,则为添加一个新的option if not conf.has_section("users"): conf.add_section("users") # 添加一个新的section, 若user已经存在,会抛出异常 conf.set(section="users", option="user1", value="A") conf.set(section="users", option="user2", value="B") conf.write(open(iniFileUrl, 'r+')) if __name__ == '__main__': read_ini_file() write_ini_file()
输出为:
[code]"""程序输入""" 当前配置文件中的section内容有: ['Section_A', 'Section_B'] Section_A中所有的键options: ['a_key1', 'a_key2'] Section_B中所有的键options: ['host', 'username', 'password'] Section_A中所有的键值对为: [('a_key1', '10 # 这是注释1'), ('a_key2', '20')] Section_B中所有的键值对为: [('host', '127.0.0.1'), ('username', 'xxxxxx'), ('password', '123456')] Section_A中键a_key1的值为: 10 # 这是注释1 Section_B中键host的值为: 127.0.0.1 """原来的config.ini文件内容改变如下""" [Section_A] a_key1 = 1000 a_key2 = 20 [Section_B] host = 127.0.0.1 username = xxxxxx password = 123456 available = True [users] user1 = A user2 = B
2.模块configobj
[code]# -*- coding: utf-8 -*- from configobj import ConfigObj """ configobj操作section相当于获得了一个字典,然后可以通过键来获取对应的值 相比于configparser,对注释的解读较好,可以放在同一行 但是配置文件中的键值对只能以key=value的形式存在,不能以key:value的形式存在 """ conf = ConfigObj("config.ini", encoding='UTF-8') """读取""" print("Section_A的内容为:", conf["Section_A"]) # 返回一个section对象, print("对应的类型为:", type(conf["Section_A"])) print("Section_B中键为host的值为:", conf["Section_B"]["host"]) # 方式1 print("Section_B中键为username的值为:", conf["Section_B"].get("username")) # 方式2 """修改""" conf["Section_B"]["host"] = "0.0.0.0" print("修改后Section_B中的键host的值为:", conf["Section_B"]["host"]) conf.write() """添加section""" conf["Tel"] = {} conf["Tel"]["周杰伦"] = "13888888888" conf["Tel"]["李健"] = "13999999999" conf.write() print("Tel中键为周杰伦对应的值为:", conf["Tel"]["周杰伦"]) print("Tel中键为李健对应的值为:", conf["Tel"]["李健"]) """删除""" del conf["Tel"]["李健"] # 只会删除option李健 del conf["Tel"] # 会删除section tel conf.write()
输出为:
[code]Section_A的内容为: {'a_key1': '10', 'a_key2': '20'} 对应的类型为: <class 'configobj.Section'> Section_B中键为host的值为: 127.0.0.1 Section_B中键为username的值为: xxxxxx 修改后Section_B中的键host的值为: 0.0.0.0 Tel中键为周杰伦对应的值为: 13888888888 Tel中键为李健对应的值为: 13999999999
这两个模块还有一些其他的用法,去试试吧,少年。。。
阅读更多相关文章推荐
- python对配置文件.ini进行增删改查操作的方法示例
- python操作配置文件ini的方法
- python 提供INI配置文件的操作 ConfigParser
- python 3 ini配置文件操作
- Python模块configparser(操作配置文件ini)
- Delphi TIniFile 类 操作ini配置文件
- Delphi TIniFile 类 操作ini配置文件
- python读写ini配置文件方法实例分析
- Delphi TIniFile 类 操作ini配置文件
- MFC操作ini配置文件
- 配置文件( .INI )操作
- python之haproxy配置文件操作(第三天)
- C# 操作ini配置文件帮助类
- Python ini文件操作
- Python 操作配置文件
- 如何使用Python3读取配置文件(ini)
- python读写ini配置文件方法实例分析
- 如何使用Python3读写INI配置文件
- C# 操作INI配置文件
- Delphi TIniFile 类 操作ini配置文件