configparser
内置在Python标准库中,用来处理类似Windows ini格式的配置文件。
Py2中该模块名为ConfigParser,Py3中更名为configparser
配置文件语法
配置文件语法如下:
- 分隔符支持
:
和=
,如key:value
或key=value
- 注释支持
;
和#
- *value支持多行
- *可以只有key,没有value
- *value支持引用
读取配置文件方法
读取配置文件有4种方法:
read方法,参数为文件名或包含文件名的列表
>>> import configparser >>> conf = configparser.ConfigParser() >>> conf.read('config_01.ini') #读取单个配置文件 ['config_01.ini'] >>> config_files = ['config_01.ini', 'config_02.ini', 'config_03.ini'] >>> conf.read(config_files) #读取多个配置文件 ['config_01.ini', 'config_02.ini', 'config_03.ini'] >>> conf.sections() ['config_01.section', 'config_02.section', 'config_03.section']
read_dict方法,参数为字典
>>> import configparser >>> conf = configparser.ConfigParser() >>> conf.read_dict(dict( ... section1=dict(k11='v11', k12='v12'), ... section2=dict(k21='v21', k22='v22'), ... section3=dict(k31='v31', k32='v32'), ... )) >>> conf.sections() ['section1', 'section2', 'section3']
read_file方法,参数为文件句柄
>>> import configparser >>> conf = configparser.ConfigParser() >>> conf.read_file(open('config_01.ini')) >>> conf.sections() ['db.account']
read_string方法,参数为字符串
>>> import configparser >>> conf.read_string('''[db.account]) ... host = 127.0.0.1 ... port = 5432 ... user = user ... password = 123456 ... database = account''') >>> conf.sections() ['db.account']
configparser常用方法
>>> import configparser
>>> conf = configparser.ConfigParser()
# 读取不存在的配置文件
>>> conf.read('not_exists.ini')
[]
# 读取存在的配置文件
>>> conf.read_string('''[db.account]
host = 127.0.0.1
port = 5432
user = user
auto_commit = off
password : 123456
database : account''')
# 列出全部section
>>> conf.sections()
['db.account']
# 指定section
>>> conf['db.account']
<Section: db.account>
# 判断section是否存在
>>> conf.has_section('db.account')
True
# 列出指定section全部key
>>> conf.options('db.account')
['host', 'port', 'user', 'password', 'database']
# 获取指定section.key
>>> conf['db.account']['host']
'127.0.0.1'
# 判断指定section是否存在key
>>> conf.has_option('db.account','user')
True
# 获取指定section.key
>>> conf.get('db.account','port')
'5432'
# 指定key不存在时返回值
>>> conf.get('db.account', 'username', fallback='username不存在')
'username不存在'
# 将value转为int
>>> conf.getint('db.account','port')
5432
# 将value转为float
>>> conf.getfloat('db.account','port')
5432.0
# 将value转为boolean
# '1', 'yes', 'true', 'on'将返回True
# '0', 'no', 'false', 'off'将返回False
>>> conf.getboolean('db.account','auto_commit')
False
# 指定key不存在时返回值
>>> conf.getboolean('db.account','internal', fallback=True)
True
# 列出指定section全部key和value
>>> conf.items('db.account')
[('host', '127.0.0.1'), ('port', '5432'), ('user', 'user'), ('auto_commit', 'off'), ('password', '123456'), ('database', 'account')]
configparser配置引用
# config.ini
[project]
home_path = /home/deploy/account-server
log_path = ${home_path}/log
[logging]
level = DEBUG
filename = ${project:log_path}/log/app.user-center.log
rotate_type = DATE
when = midnight
internal = 1
multiprocess = True
>>> import configparser
>>> conf = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
>>> conf.read('config.ini')
>>> conf.sections()
['project', 'logging']
>>> conf.items('project')
[('home_path', '/home/deploy/account-server'), ('log_path', '/home/deploy/account-server/log')]
>>> conf.items('logging')
[('level', 'DEBUG'), ('filename', '/home/deploy/account-server/log/log/app.user-center.log'), ('rotate_type', 'DATE'), ('when', 'midnight'), ('internal', '1'), ('multiprocess', 'True')]
configparser多行配置、空配置等
# config.ini
[feature]
multiline_value = 我是谁, 我从哪里来
我要到哪里去
key_without_value
empty string value here =
>>> import configparser
>>> conf = configparser.ConfigParser(allow_no_value=True)
>>> conf.read('config.ini')
>>> conf.sections()
['feature']
>>> conf.items('feature')
>>> conf.items('feature')
[('multiline_value', '我是谁, 我从哪里来\n我要到哪里去'), ('key_without_value', ''), ('empty string value here', '')]
configparser写配置文件
configparser
不仅可以用来读取配置文件,还可以写配置文件。
>>> import configparser
>>> conf = configparser.ConfigParser()
>>> conf.add_section('section1')
>>> conf.set('section1', 'name', 'Wayde')
>>> conf.set('section1', 'password', '123456')
>>> conf.add_section('section2')
>>> conf.set('section2', 'name', 'Peter')
>>> conf.set('section2', 'password', '654321')
>>> conf.write(open('config.ini','w+'))
>>> conf.remove_option('section1', 'password') # 移除key
True
>>> conf.remove_section('section2') # 移除section
True
>>> conf.clear() # 删除全部项
$ cat config.ini
[section1]
name = Wayde
password = 123456
[section2]
name = Peter
password = 654321