Python ConfigParser 教程
本文介绍Python ConfigParser,展示如何在Python中使用ConfigParser处理配置文件。ConfigParser是解析Python查询配置文件类,配置文件一般类似于Windows ini文件。通过配置文件可以让用户对应用进行个性化配置。
配置文件有多个部分或组构成,每个部分都包括一组键值对。组名称使用[]进行标识。键值对使用:或 = 进行分割。每行的注释可以使用#或;开头。
1. 读配置文件
下面先介绍如何读配置文件。
1.1. 读取配置文件属性
# db.ini
[mysql]
host = localhost
user = user7
passwd = s$cret
db = ydb
[postgresql]
host = localhost
user = user8
passwd = mypwd$7
db = testdb
db.ini中配置了两组数据。
# reading_from_file.py
#!/usr/bin/env python3
import configparser
config = configparser.ConfigParser()
config.read('db.ini')
host = config['mysql']['host']
user = config['mysql']['user']
passwd = config['mysql']['passwd']
db = config['mysql']['db']
print('MySQL configuration:')
print(f'Host: {host}')
print(f'User: {user}')
print(f'Password: {passwd}')
print(f'Database: {db}')
host2 = config['postgresql']['host']
user2 = config['postgresql']['user']
passwd2 = config['postgresql']['passwd']
db2 = config['postgresql']['db']
print('PostgreSQL configuration:')
print(f'Host: {host2}')
print(f'User: {user2}')
print(f'Password: {passwd2}')
print(f'Database: {db2}')
读配置文件,通过初始化configParser,并调用read方法:
config = configparser.ConfigParser()
config.read('db.ini')
获取mysql的属性:
host = config['mysql']['host']
user = config['mysql']['user']
passwd = config['mysql']['passwd']
db = config['mysql']['db']
获取postgreSql属性:
host2 = config['postgresql']['host']
user2 = config['postgresql']['user']
passwd2 = config['postgresql']['passwd']
db2 = config['postgresql']['db']
运行并输出结果:
$ python reading_from_file.py
MySQL configuration:
Host: localhost
User: user7
Password: s$cret
Database: ydb
PostgreSQL configuration:
Host: localhost
User: user8
Password: mypwd$7
Database: testdb
This is the output.
1.2. 读分组信息
配置数据通过分组进行组织。sections()方法获取所有分组信息,has_section()检查是否包含特定的组。
# sections.py
#!/usr/bin/env python3
import configparser
config = configparser.ConfigParser()
config.read('db.ini')
sections = config.sections()
print(f'Sections: {sections}')
sections.append('sqlite')
for section in sections:
if config.has_section(section):
print(f'Config file has section {section}')
else:
print(f'Config file does not have section {section}')
输出结果如下:
$ python sections.py
Sections: ['mysql', 'postgresql']
Config file has section mysql
Config file has section postgresql
Config file does not have section sqlite
1.3. 从字符串中读信息
从Python3.2开始,可以使用read_string()方法读配置信息:
# read_from_string.py
#!/usr/bin/env python3
import configparser
cfg_data = '''
[mysql]
host = localhost
user = user7
passwd = s$cret
db = ydb
'''
config = configparser.ConfigParser()
config.read_string(cfg_data)
host = config['mysql']['host']
user = config['mysql']['user']
passwd = config['mysql']['passwd']
db = config['mysql']['db']
print(f'Host: {host}')
print(f'User: {user}')
print(f'Password: {passwd}')
print(f'Database: {db}')
示例是中字符串中读配置信息。
1.4. 从字典类型中读配置信息
从python3.2开始,可以使用read_dict() 读字典中配置信息:
# read_from_dict.py
#!/usr/bin/env python3
import configparser
cfg_data = {
'mysql': {'host': 'localhost', 'user': 'user7',
'passwd': 's$cret', 'db': 'ydb'}
}
config = configparser.ConfigParser()
config.read_dict(cfg_data)
host = config['mysql']['host']
user = config['mysql']['user']
passwd = config['mysql']['passwd']
db = config['mysql']['db']
print(f'Host: {host}')
print(f'User: {user}')
print(f'Password: {passwd}')
print(f'Database: {db}')
配置字典结构支持嵌套,从而实现分组配置。
cfg_data = {
'mysql': {'host': 'localhost', 'user': 'user7',
'passwd': 's$cret', 'db': 'ydb'}
}
2. 写配置文件
使用write方法写配置信息。下面示例写db3.ini配置文件:
# writing.py
#!/usr/bin/env python3
import configparser
config = configparser.ConfigParser()
config.add_section('mysql')
config['mysql']['host'] = 'localhost'
config['mysql']['user'] = 'user7'
config['mysql']['passwd'] = 's$cret'
config['mysql']['db'] = 'ydb'
with open('db3.ini', 'w') as configfile:
config.write(configfile)
增加分组信息:
config.add_section('mysql')
然后设置属性:
config['mysql']['host'] = 'localhost'
config['mysql']['user'] = 'user7'
config['mysql']['passwd'] = 's$cret'
config['mysql']['db'] = 'ydb'
最后使用wirte方法写入文件。
3. 配置文件支持变量
ConfigParser支持配置文件包括变量的解析,变量语法使用%()s:
# cfg.ini
[info]
users_dir= C:\Users
name= Jano
home_dir= %(users_dir)s\%(name)s
home_dir属性使用变量。注意‘s’字符是语法的一部分。
输出结果:
$ python interpolation.py
Users directory: C:\Users
Name: Jano
Home directory: C:\Users\Jano
4. 总结
本文通过示例介绍了多种方式读取配置文件,同时介绍了写配置文件及支持变量方式设置属性值。
转载:https://blog.csdn.net/neweastsun/article/details/101151522
查看评论