1、Python2中读写配置文件
Python2 中,读写配置文件通常使用 ConfigParser
模块,它支持读取 INI 格式的文件,通过 read()
加载配置文件,使用 get()
、set()
方法读取或修改配置项,修改后可通过 write()
方法将更改保存回文件,实现对配置信息的灵活管理。
1) 写配置文件示例代码
import ConfigParser config = ConfigParser.RawConfigParser() # 在添加部分或项时,按相反的顺序添加它们 # 您希望它们在实际文件中如何显示。 # 另外,请注意使用RawConfigParser和raw # 您可以指定ConfigParser各自的set函数的#模式 # 非字符串值键内部,但将收到一个错误 # 当你试图写一个文件或当你得到它的非原始 # 模式。SafeConfigParser不允许进行这样的赋值。 config.add_section('Section1') config.set('Section1', 'an_int', '15') config.set('Section1', 'a_bool', 'true') config.set('Section1', 'a_float', '3.1415') config.set('Section1', 'baz', 'fun') config.set('Section1', 'bar', 'Python') config.set('Section1', 'foo', '%(bar)s is %(baz)s!') # Writing our configuration file to 'example.cfg' with open('example.cfg', 'wb') as configfile: config.write(configfile)
2) 读配置文件示例代码
使用RawConfigParser()
import ConfigParser config = ConfigParser.RawConfigParser() config.read('example.cfg') # getfloat() 如果该值不是一个浮点数,则引发一个异常 # getint()和getboolean()也对它们各自的类型执行此操作 a_float = config.getfloat('Section1', 'a_float') an_int = config.getint('Section1', 'an_int') print a_float + an_int # 注意,下一个输出没有插入'%(bar)s'或'%(baz)s'。 # 这是因为我们使用的是RawConfigParser()。 if config.getboolean('Section1', 'a_bool'): print config.get('Section1', 'foo')
使用ConfigParser()
import ConfigParser config = ConfigParser.ConfigParser() config.read('example.cfg') # 如果您希望使用原始模式,请设置get的第三个可选参数为1。 print config.get('Section1', 'foo', 0) # -> "Python is fun!" print config.get('Section1', 'foo', 1) # -> "%(bar)s is %(baz)s!" # 可选的第四个参数是一个dict,其成员将接受 # 插补优先。 print config.get('Section1', 'foo', 0, {'bar': 'Documentation', 'baz': 'evil'})
参考文档:https://docs.python.org/2.7/library/configparser.html
2、Python3中读写配置文件
Python3 中,读写配置文件通常使用 configparser
模块,该模块支持读取 .ini
格式的配置,通过 read()
加载文件,使用 get()
、set()
操作配置项,修改后通过 write()
方法将更改保存回文件,实现对配置信息的结构化管理。
1) 写入配置文件
import configparser config = configparser.RawConfigParser() # 请注意,使用RawConfigParser的set函数,您可以分配 # 非字符串值键内部,但将收到一个错误时 # 试图写入文件或在非原始模式下写入文件。设置 # 使用映射协议或ConfigParser的set()不允许使用#值 # 这样的分配将会发生。 config.add_section('Section1') config.set('Section1', 'an_int', '15') config.set('Section1', 'a_bool', 'true') config.set('Section1', 'a_float', '3.1415') config.set('Section1', 'baz', 'fun') config.set('Section1', 'bar', 'Python') config.set('Section1', 'foo', '%(bar)s is %(baz)s!') # 写进配置文件 'example.cfg' with open('example.cfg', 'w') as configfile: config.write(configfile)
2) 读取配置文件
使用RawConfigParser()
import configparser config = configparser.RawConfigParser() config.read('example.cfg') # getfloat() 如果该值不是一个浮点数,则引发一个异常 # getint()和getboolean()也对它们各自的类型执行此操作 a_float = config.getfloat('Section1', 'a_float') an_int = config.getint('Section1', 'an_int') print(a_float + an_int) # 注意,下一个输出没有插入'%(bar)s'或'%(baz)s'。 # 这是因为我们使用的是RawConfigParser()。 if config.getboolean('Section1', 'a_bool'): print(config.get('Section1', 'foo'))
使用ConfigParser()
import configparser cfg = configparser.ConfigParser() cfg.read('example.cfg') # 如果您想禁用get(),可以将它的可选*raw*参数设置为True # 单get操作中的插值表达式。 # -> "Python is fun!" print(cfg.get('Section1', 'foo', raw=False)) # -> "%(bar)s is %(baz)s!" print(cfg.get('Section1', 'foo', raw=True)) # 可选的*vars*参数是一个dict,其成员将接受 # 插补优先。 print(cfg.get('Section1', 'foo', vars={'bar': 'Documentation','baz': 'evil'})) # 可选的*回退*参数可用于提供回退值 print(cfg.get('Section1', 'foo')) # -> "Python is fun!" print(cfg.get('Section1', 'foo', fallback='Monty is not.')) # -> "Python is fun!" print(cfg.get('Section1', 'monster', fallback='No such things as monsters.')) # -> "No such things as monsters." # 没内容print(cfg.get('Section1', 'monster')) # 将引发NoOptionError # 但是我们也可以用: print(cfg.get('Section1', 'monster', fallback=None)) # -> None
默认值在这两种configparser中都可用。如果在其他地方没有定义所使用的选项,则将它们用于插值
import configparser # 例如"bar"和"baz"分别默认为"Life"和"hard" config = configparser.ConfigParser({'bar': 'Life', 'baz': 'hard'}) config.read('example.cfg') print(config.get('Section1', 'foo')) # -> "Python is fun!" config.remove_option('Section1', 'bar') config.remove_option('Section1', 'baz') print(config.get('Section1', 'foo')) # -> "Life is hard!"