原文: http://zetcode.com/python/configparser/

Python ConfigParser教程显示了如何使用ConfigParser在 Python 中使用配置文件。

Python ConfigParser

ConfigParser是一个 Python 类,为 Python 程序实现基本的配置语言。 它提供类似于 Microsoft Windows INI 文件的结构。 ConfigParser允许编写可由最终用户轻松定制的 Python 程序。

配置文件由各部分组成,后跟选项的键/值对。 段名用[]字符分隔。 这些对用:=隔开。 注释以#;开头。

Python ConfigParser读取文件

在第一个示例中,我们从文件中读取配置数据。

db.ini

  1. [mysql]
  2. host = localhost
  3. user = user7
  4. passwd = s$cret
  5. db = ydb
  6. [postgresql]
  7. host = localhost
  8. user = user8
  9. passwd = mypwd$7
  10. db = testdb

我们有两部分配置数据。

reading_from_file.py

  1. #!/usr/bin/env python3
  2. import configparser
  3. config = configparser.ConfigParser()
  4. config.read('db.ini')
  5. host = config['mysql']['host']
  6. user = config['mysql']['user']
  7. passwd = config['mysql']['passwd']
  8. db = config['mysql']['db']
  9. print('MySQL configuration:')
  10. print(f'Host: {host}')
  11. print(f'User: {user}')
  12. print(f'Password: {passwd}')
  13. print(f'Database: {db}')
  14. host2 = config['postgresql']['host']
  15. user2 = config['postgresql']['user']
  16. passwd2 = config['postgresql']['passwd']
  17. db2 = config['postgresql']['db']
  18. print('PostgreSQL configuration:')
  19. print(f'Host: {host2}')
  20. print(f'User: {user2}')
  21. print(f'Password: {passwd2}')
  22. print(f'Database: {db2}')

该示例读取 MySQL 和 PostgreSQL 的配置数据。

  1. config = configparser.ConfigParser()
  2. config.read('db.ini')

我们启动ConfigParser并使用read()读取文件。

  1. host = config['mysql']['host']
  2. user = config['mysql']['user']
  3. passwd = config['mysql']['passwd']
  4. db = config['mysql']['db']

我们从 mysql 部分访问选项。

  1. host2 = config['postgresql']['host']
  2. user2 = config['postgresql']['user']
  3. passwd2 = config['postgresql']['passwd']
  4. db2 = config['postgresql']['db']

我们从 postgresql 部分访问选项。

  1. $ python reading_from_file.py
  2. MySQL configuration:
  3. Host: localhost
  4. User: user7
  5. Password: s$cret
  6. Database: ydb
  7. PostgreSQL configuration:
  8. Host: localhost
  9. User: user8
  10. Password: mypwd$7
  11. Database: testdb

这是输出。

Python ConfigParser部分

配置数据分为几部分。 sections()读取所有部分,has_section()检查是否存在指定的部分。

sections.py

  1. #!/usr/bin/env python3
  2. import configparser
  3. config = configparser.ConfigParser()
  4. config.read('db.ini')
  5. sections = config.sections()
  6. print(f'Sections: {sections}')
  7. sections.append('sqlite')
  8. for section in sections:
  9. if config.has_section(section):
  10. print(f'Config file has section {section}')
  11. else:
  12. print(f'Config file does not have section {section}')

该示例适用于各节。

  1. $ python sections.py
  2. Sections: ['mysql', 'postgresql']
  3. Config file has section mysql
  4. Config file has section postgresql
  5. Config file does not have section sqlite

这是输出。

Python ConfigParser从字符串读取

从 Python 3.2 开始,我们可以使用read_string()方法从字符串读取配置数据。

read_from_string.py

  1. #!/usr/bin/env python3
  2. import configparser
  3. cfg_data = '''
  4. [mysql]
  5. host = localhost
  6. user = user7
  7. passwd = s$cret
  8. db = ydb
  9. '''
  10. config = configparser.ConfigParser()
  11. config.read_string(cfg_data)
  12. host = config['mysql']['host']
  13. user = config['mysql']['user']
  14. passwd = config['mysql']['passwd']
  15. db = config['mysql']['db']
  16. print(f'Host: {host}')
  17. print(f'User: {user}')
  18. print(f'Password: {passwd}')
  19. print(f'Database: {db}')

该示例从字符串读取配置。

Python ConfigParser从字典中读取

从 Python 3.2 开始,我们可以使用read_dict()方法从字典中读取配置数据。

read_from_dict.py

  1. #!/usr/bin/env python3
  2. import configparser
  3. cfg_data = {
  4. 'mysql': {'host': 'localhost', 'user': 'user7',
  5. 'passwd': 's$cret', 'db': 'ydb'}
  6. }
  7. config = configparser.ConfigParser()
  8. config.read_dict(cfg_data)
  9. host = config['mysql']['host']
  10. user = config['mysql']['user']
  11. passwd = config['mysql']['passwd']
  12. db = config['mysql']['db']
  13. print(f'Host: {host}')
  14. print(f'User: {user}')
  15. print(f'Password: {passwd}')
  16. print(f'Database: {db}')

该示例从 Python 字典读取配置。

  1. cfg_data = {
  2. 'mysql': {'host': 'localhost', 'user': 'user7',
  3. 'passwd': 's$cret', 'db': 'ydb'}
  4. }

键是部分名称,值是带有该部分中存在的键和值的字典。

Python ConfigParser写入

write()方法写入配置数据。

writing.py

  1. #!/usr/bin/env python3
  2. import configparser
  3. config = configparser.ConfigParser()
  4. config.add_section('mysql')
  5. config['mysql']['host'] = 'localhost'
  6. config['mysql']['user'] = 'user7'
  7. config['mysql']['passwd'] = 's$cret'
  8. config['mysql']['db'] = 'ydb'
  9. with open('db3.ini', 'w') as configfile:
  10. config.write(configfile)

该示例将配置数据写入db3.ini文件。

  1. config.add_section('mysql')

首先,我们用add_section()添加一个部分。

  1. config['mysql']['host'] = 'localhost'
  2. config['mysql']['user'] = 'user7'
  3. config['mysql']['passwd'] = 's$cret'
  4. config['mysql']['db'] = 'ydb'

然后我们设置选项。

  1. with open('db3.ini', 'w') as configfile:
  2. config.write(configfile)

最后,我们用write()写入数据。

Python ConfigParser插值

ConfigParser允许在配置文件中使用插值。 它使用%()语法。

cfg.ini

  1. [info]
  2. users_dir= C:\Users
  3. name= Jano
  4. home_dir= %(users_dir)s\%(name)s

我们用插值法构建home_dir。 请注意,"s"字符是语法的一部分。

interpolation.py

  1. #!/usr/bin/env python3
  2. import configparser
  3. config = configparser.ConfigParser()
  4. config.read('cfg.ini')
  5. users_dir = config['info']['users_dir']
  6. name = config['info']['name']
  7. home_dir = config['info']['home_dir']
  8. print(f'Users directory: {users_dir}')
  9. print(f'Name: {name}')
  10. print(f'Home directory: {home_dir}')

该示例读取值并打印出来。

  1. $ python interpolation.py
  2. Users directory: C:\Users
  3. Name: Jano
  4. Home directory: C:\Users\Jano

这是输出。

在本教程中,我们使用ConfigParser处理 Python 中的配置数据。

您可能也会对以下相关教程感兴趣: Python 教程Python 哈希教程或列出所有 Python 教程