PythonConfigParser教程展示了如何在Python中使用ConfigParser处理配置文件。
Python配置分析器
ConfigParser
是一个Python类,它实现了Python程序的基本配置语言。它提供类似于MicrosoftWindowsINI文件的结构。ConfigParser
允许编写最终用户可以轻松自定义的Python程序。
配置文件由后跟键/值对选项的部分组成。部分名称由[]
字符分隔。这些对用:
或=
分隔。注释以#
或;
开头。
PythonConfigParser读取文件
在第一个示例中,我们从文件中读取配置数据。
[mysql] host = localhost user = user7 passwd = s$cret db = ydb [postgresql] host = localhost user = user8 passwd = mypwd$7 db = testdb
我们有两部分配置数据。
#!/usr/bin/python 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}')
该示例读取MySQL和PostgreSQL的配置数据。
config = configparser.ConfigParser() config.read('db.ini')
我们启动ConfigParser
并使用read
读取文件。
host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db']
我们从mysql部分访问选项。
host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db']
我们从postgresql部分访问选项。
$ 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
PythonConfigParser部分
配置数据被组织成多个部分。sections
读取所有部分,has_section
检查是否存在指定的部分。
#!/usr/bin/python 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
PythonConfigParser从字符串中读取
从Python3.2开始,我们可以使用read_string
方法从字符串中读取配置数据。
#!/usr/bin/python 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}')
该示例从字符串中读取配置。
PythonConfigParser从字典中读取
从Python3.2开始,我们可以使用read_dict
方法从字典中读取配置数据。
#!/usr/bin/python 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}')
该示例从Python字典中读取配置。
cfg_data = { 'mysql': {'host': 'localhost', 'user': 'user7', 'passwd': 's$cret', 'db': 'ydb'} }
键是部分名称,值是包含部分中存在的键和值的字典。
PythonConfigParser编写
write
方法写入配置数据。
#!/usr/bin/python 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)
该示例将配置数据写入db3.ini
文件。
config.add_section('mysql')
首先,我们使用add_section
添加一个部分。
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)
最后,我们用write
写入数据。
PythonConfigParser插值
ConfigParser
允许在配置文件中使用插值。它使用%
语法。
[info] users_dir= C:\Users name= Jano home_dir= %(users_dir)s\%(name)s
我们用插值构建home_dir
。请注意,“s”字符是语法的一部分。
#!/usr/bin/python import configparser config = configparser.ConfigParser() config.read('cfg.ini') users_dir = config['info']['users_dir'] name = config['info']['name'] home_dir = config['info']['home_dir'] print(f'Users directory: {users_dir}') print(f'Name: {name}') print(f'Home directory: {home_dir}')
该示例读取值并打印它们。
$ python interpolation.py Users directory: C:\Users Name: Jano Home directory: C:\Users\Jano
在本教程中,我们使用ConfigParser
在Python中处理配置数据。
列出所有Python教程。