企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Python `ConfigParser`教程 > 原文: [http://zetcode.com/python/configparser/](http://zetcode.com/python/configparser/) Python `ConfigParser`教程显示了如何使用`ConfigParser`在 Python 中使用配置文件。 ## Python `ConfigParser` `ConfigParser`是一个 Python 类,为 Python 程序实现基本的配置语言。 它提供类似于 Microsoft Windows INI 文件的结构。 `ConfigParser`允许编写可由最终用户轻松定制的 Python 程序。 配置文件由各部分组成,后跟选项的键/值对。 段名用`[]`字符分隔。 这些对用`:`或`=`隔开。 注释以`#`或`;`开头。 ## Python `ConfigParser`读取文件 在第一个示例中,我们从文件中读取配置数据。 `db.ini` ```py [mysql] host = localhost user = user7 passwd = s$cret db = ydb [postgresql] host = localhost user = user8 passwd = mypwd$7 db = testdb ``` 我们有两部分配置数据。 `reading_from_file.py` ```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}') ``` 该示例读取 MySQL 和 PostgreSQL 的配置数据。 ```py config = configparser.ConfigParser() config.read('db.ini') ``` 我们启动`ConfigParser`并使用`read()`读取文件。 ```py host = config['mysql']['host'] user = config['mysql']['user'] passwd = config['mysql']['passwd'] db = config['mysql']['db'] ``` 我们从 mysql 部分访问选项。 ```py host2 = config['postgresql']['host'] user2 = config['postgresql']['user'] passwd2 = config['postgresql']['passwd'] db2 = config['postgresql']['db'] ``` 我们从 postgresql 部分访问选项。 ```py $ 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 ``` 这是输出。 ## Python `ConfigParser`部分 配置数据分为几部分。 `sections()`读取所有部分,`has_section()`检查是否存在指定的部分。 `sections.py` ```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}') ``` 该示例适用于各节。 ```py $ python sections.py Sections: ['mysql', 'postgresql'] Config file has section mysql Config file has section postgresql Config file does not have section sqlite ``` 这是输出。 ## Python `ConfigParser`从字符串读取 从 Python 3.2 开始,我们可以使用`read_string()`方法从字符串读取配置数据。 `read_from_string.py` ```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}') ``` 该示例从字符串读取配置。 ## Python `ConfigParser`从字典中读取 从 Python 3.2 开始,我们可以使用`read_dict()`方法从字典中读取配置数据。 `read_from_dict.py` ```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}') ``` 该示例从 Python 字典读取配置。 ```py cfg_data = { 'mysql': {'host': 'localhost', 'user': 'user7', 'passwd': 's$cret', 'db': 'ydb'} } ``` 键是部分名称,值是带有该部分中存在的键和值的字典。 ## Python `ConfigParser`写入 `write()`方法写入配置数据。 `writing.py` ```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) ``` 该示例将配置数据写入`db3.ini`文件。 ```py config.add_section('mysql') ``` 首先,我们用`add_section()`添加一个部分。 ```py config['mysql']['host'] = 'localhost' config['mysql']['user'] = 'user7' config['mysql']['passwd'] = 's$cret' config['mysql']['db'] = 'ydb' ``` 然后我们设置选项。 ```py with open('db3.ini', 'w') as configfile: config.write(configfile) ``` 最后,我们用`write()`写入数据。 ## Python `ConfigParser`插值 `ConfigParser`允许在配置文件中使用插值。 它使用`%()`语法。 `cfg.ini` ```py [info] users_dir= C:\Users name= Jano home_dir= %(users_dir)s\%(name)s ``` 我们用插值法构建`home_dir`。 请注意,`"s"`字符是语法的一部分。 `interpolation.py` ```py #!/usr/bin/env python3 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}') ``` 该示例读取值并打印出来。 ```py $ python interpolation.py Users directory: C:\Users Name: Jano Home directory: C:\Users\Jano ``` 这是输出。 在本教程中,我们使用`ConfigParser`处理 Python 中的配置数据。 您可能也会对以下相关教程感兴趣: [Python 教程](/lang/python/), [Python 哈希教程](/python/hashing/)或列出[所有 Python 教程](/all/#python)。