主要介绍python3中的ConfigParser模块的使用,该模块主要被用来读写配置文件。
安装模块:pip3 install configparser
root@ranxf:/usr/lib/python3/dist-packages# pip3 install configparser
Collecting configparser
Downloading configparser-3.5.0.tar.gz
Building wheels for collected packages: configparser
Running setup.py bdist_wheel for configparser ... done
Stored in directory: /root/.cache/pip/wheels/1c/bd/b4/277af3f6c40645661b4cd1c21df26aca0f2e1e9714a1d4cda8
Successfully built configparser
Installing collected packages: configparser
Successfully installed configparser-3.5.0
基本的读取配置文件
-read(filename) 直接读取文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该section的所有option-items(section) 得到该section的所有键值对-get(section,option) 得到section中option的值,返回为string类型-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
基本的写入配置文件
-add_section(section) 添加一个新的section-set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
在对配置文件进行读写操作前,我们需要先进行以下两个操作:
1、实例化ConfigParser对象:
# 实例化configParser对象
cf = configparser.ConfigParser()
2、读取配置文件
# 读取config.ini文件
cf.read(config.ini)
然后进行配置文件的读取操作。
以get为例,示例代码如下:
# 定义方法,获取config分组下指定name的值
def getConfigValue(self, name):
value = self.cf.get("config", name)
return value
# 定义方法,获取cmd分组下指定name的值
def getCmdValue(self, name):
value = self.cf.get("cmd", name)
return value
通过get(section, option)方法,可以获取指定分组下指定名称的值,其他方法类似,可参照着尝试。
基本的写入操作:
-write(fp)将config对象写入至某个 .init 格式的文件Write an .ini-format representation of the configuration state.
-add_section(section)添加一个新的section
-set( section, option, value 对section中的option进行设置,需要调用write将内容写入配置文件
-remove_section(section)删除某个 section
-remove_option(section, option)
以set(section, option, value)为例,示例代码如下:
# 定义方法,修改config分组下指定name的值value
def setConfigValue(self, name, value):
cfg = self.cf.set("config", name, value)
fp = open(r'config.ini', 'w')
cfg.write(fp)
其他方法可以自行尝试。
配置文件中的名字是不区分大小写的,如下两个是等价的:
# 不区分大小写,以下两个等价,都获取appActivity的值
self.cf.get("config", "appActivity")
self.cf.get("config", "APPACTIVITY")
在解析时,getboolean()方法查找任何可行的值,例如以下几个都是等价的:
# 以下取得的值都是等价的为ture
[log]
log_error=true
log_error=TRUE
log_error=1
log_error=yes
实例代码1:
config.conf
[section1]
name=test
func=mainhost
[section2]
ip= 192.168.2.170
demo.py
importconfigparser
conf=configparser.ConfigParser()
conf.read("./config.conf")#获取指定的section, 指定的option的值
name = conf.get("section1", "name")print(name)
func= conf.get("section1", "func")print(func)#获取所有的section
sections =conf.sections()print(sections)#写配置文件#更新指定section, option的值
conf.set("section2", "port", "8080")#写入指定section, 增加新option的值
conf.set("section2", "IEPort", "80")#添加新的 section
conf.add_section("new_section")
conf.set("new_section", "new_option", "https://github.com/Ranxf/")#写回配置文件
conf.write(open("./config.conf", "w"))
实例代码2:
config.conf
[section1]
name = test
func = mainhost
[section2]
ip = 10.1.1.61
port = 8080
demo2.py(读取配置文件)
'''Date:2017.12.26
Author:Ranxf'''
importconfigparser
conf=configparser.ConfigParser()
conf.read("./config.conf")#获取指定的section, 指定的option的值
name = conf.get("section1", "name")print(name)
func= conf.get("section1", "func")print(func)#获取所有的section
sections =conf.sections()print(sections)
demo3.py(写入配置文件):
'''Date:2017.12.26
Author:Ranxf'''
importconfigparser
conf=configparser.ConfigParser()
conf.read('config.conf')
conf.set("section1", "name", "ranxf") #修改指定section 的option
conf.set("section1", "age", "21") #增加指定section 的option
conf.add_section("section3") #增加section
conf.set("section3", "site", "hehe.net") #给新增的section 写入option
conf.write(open('config.conf', 'w'))
写入后的配置文件
[section1]
name = interfacetest
func = mainhost
age = 21
[section2]
ip = 10.1.1.61
port = 8080
[section3]
site = hehe.net
参考: