configparser —- 配置文件解析器

源代码: Lib/configparser.py


此模块提供了它实现一种基本配置语言 ConfigParser 类,这种语言所提供的结构与 Microsoft Windows INI 文件的类似。 你可以使用这种语言来编写能够由最终用户来自定义的 Python 程序。

注解

这个库 并不 能够解析或写入在 Windows Registry 扩展版本 INI 语法中所使用的值-类型前缀。

参见

模块 shlex

支持创建可被用作应用配置文件的替代的 Unix 终端式微语言。

模块 json

json 模块实现了一个 JavaScript 语法的子集,它也可被用于这种目的。

快速起步

让我们准备一个非常基本的配置文件,它看起来是这样的:

  1. [DEFAULT]
  2. ServerAliveInterval = 45
  3. Compression = yes
  4. CompressionLevel = 9
  5. ForwardX11 = yes
  6. [bitbucket.org]
  7. User = hg
  8. [topsecret.server.com]
  9. Port = 50022
  10. ForwardX11 = no

INI 文件的结构描述见 以下章节。 总的来说,这种文件由多个节组成,每个节包含多个带有值的键。 configparser 类可以读取和写入这种文件。 让我们先通过程序方式来创建上述的配置文件。

  1. >>> import configparser
  2. >>> config = configparser.ConfigParser()
  3. >>> config['DEFAULT'] = {'ServerAliveInterval': '45',
  4. ... 'Compression': 'yes',
  5. ... 'CompressionLevel': '9'}
  6. >>> config['bitbucket.org'] = {}
  7. >>> config['bitbucket.org']['User'] = 'hg'
  8. >>> config['topsecret.server.com'] = {}
  9. >>> topsecret = config['topsecret.server.com']
  10. >>> topsecret['Port'] = '50022' # mutates the parser
  11. >>> topsecret['ForwardX11'] = 'no' # same here
  12. >>> config['DEFAULT']['ForwardX11'] = 'yes'
  13. >>> with open('example.ini', 'w') as configfile:
  14. ... config.write(configfile)
  15. ...

如你所见,我们可以把配置解析器当作一个字典来处理。 两者确实存在差异,将在后文说明,但是其行为非常接近于字典所具有一般行为。

现在我们已经创建并保存了一个配置文件,让我们再将它读取出来并探究其中包含的数据。

  1. >>> config = configparser.ConfigParser()
  2. >>> config.sections()
  3. []
  4. >>> config.read('example.ini')
  5. ['example.ini']
  6. >>> config.sections()
  7. ['bitbucket.org', 'topsecret.server.com']
  8. >>> 'bitbucket.org' in config
  9. True
  10. >>> 'bytebong.com' in config
  11. False
  12. >>> config['bitbucket.org']['User']
  13. 'hg'
  14. >>> config['DEFAULT']['Compression']
  15. 'yes'
  16. >>> topsecret = config['topsecret.server.com']
  17. >>> topsecret['ForwardX11']
  18. 'no'
  19. >>> topsecret['Port']
  20. '50022'
  21. >>> for key in config['bitbucket.org']:
  22. ... print(key)
  23. user
  24. compressionlevel
  25. serveraliveinterval
  26. compression
  27. forwardx11
  28. >>> config['bitbucket.org']['ForwardX11']
  29. 'yes'

正如我们在上面所看到的,相关的 API 相当直观。 唯一有些神奇的地方是 DEFAULT 小节,它为所有其他小节提供了默认值 1。 还要注意小节中的键大小写不敏感并且会存储为小写形式 1

将多个配置读入单个 ConfigParser 是可能的,其中最近添加的配置具有最高优先级。 任何冲突的键都会从更近的配置获取并且先前存在的键会被保留。

  1. >>> another_config = configparser.ConfigParser()
  2. >>> another_config.read('example.ini')
  3. ['example.ini']
  4. >>> another_config['topsecret.server.com']['Port']
  5. '50022'
  6. >>> another_config.read_string("[topsecret.server.com]\nPort=48484")
  7. >>> another_config['topsecret.server.com']['Port']
  8. '48484'
  9. >>> another_config.read_dict({"topsecret.server.com": {"Port": 21212}})
  10. >>> another_config['topsecret.server.com']['Port']
  11. '21212'
  12. >>> another_config['topsecret.server.com']['ForwardX11']
  13. 'no'

此行为等价于一次 ConfigParser.read() 调用并向 filenames 形参传入多个文件。

支持的数据类型

配置解析器并不会猜测配置文件中值的类型,而总是将它们在内部存储为字符串。 这意味着如果你需要其他数据类型,你应当自己来转换:

  1. >>> int(topsecret['Port'])
  2. 50022
  3. >>> float(topsecret['CompressionLevel'])
  4. 9.0

由于这种任务十分常用,配置解析器提供了一系列便捷的获取方法来处理整数、浮点数和布尔值。 最后一个类型的处理最为有趣,因为简单地将值传给 bool() 是没有用的,bool('False') 仍然会是 True。 为解决这个问题配置解析器还提供了 getboolean()。 这个方法对大小写不敏感并可识别 'yes'/'no', 'on'/'off', 'true'/'false''1'/'0' 1 等布尔值。 例如:

  1. >>> topsecret.getboolean('ForwardX11')
  2. False
  3. >>> config['bitbucket.org'].getboolean('ForwardX11')
  4. True
  5. >>> config.getboolean('bitbucket.org', 'Compression')
  6. True

除了 getboolean(),配置解析器还提供了同类的 getint()getfloat() 方法。 你可以注册你自己的转换器并或是定制已提供的转换器。 1

回退值

与字典类似,你可以使用某个小节的 get() 方法来提供回退值:

  1. >>> topsecret.get('Port')
  2. '50022'
  3. >>> topsecret.get('CompressionLevel')
  4. '9'
  5. >>> topsecret.get('Cipher')
  6. >>> topsecret.get('Cipher', '3des-cbc')
  7. '3des-cbc'

请注意默认值会优先于回退值。 例如,在我们的示例中 'CompressionLevel' 键仅在 'DEFAULT' 小节被指定。 如果你尝试在 'topsecret.server.com' 小节获取它,我们将总是获取到默认值,即使我们指定了一个回退值:

  1. >>> topsecret.get('CompressionLevel', '3')
  2. '9'

还需要注意的一点是解析器层级的 get() 方法提供了自定义的更复杂接口,它被维护用于向下兼容。 当使用此方法时,回退值可以通过 fallback 仅限关键字参数来提供:

  1. >>> config.get('bitbucket.org', 'monster',
  2. ... fallback='No such things as monsters')
  3. 'No such things as monsters'

同样的 fallback 参数也可在 getint(), getfloat()getboolean() 方法中使用,例如:

  1. >>> 'BatchMode' in topsecret
  2. False
  3. >>> topsecret.getboolean('BatchMode', fallback=True)
  4. True
  5. >>> config['DEFAULT']['BatchMode'] = 'no'
  6. >>> topsecret.getboolean('BatchMode', fallback=True)
  7. False

受支持的 INI 文件结构

A configuration file consists of sections, each led by a [section] header, followed by key/value entries separated by a specific string (= or : by default 1). By default, section names are case sensitive but keys are not 1. Leading and trailing whitespace is removed from keys and values. Values can be omitted if the parser is configured to allow it 1, in which case the key/value delimiter may also be left out. Values can also span multiple lines, as long as they are indented deeper than the first line of the value. Depending on the parser’s mode, blank lines may be treated as parts of multiline values or ignored.

配置文件可以包含注释,要带有指定字符前缀 (默认为 #; 1)。 注释可以单独出现于原本的空白行,并可使用缩进。 1

例如:

  1. [Simple Values]
  2. key=value
  3. spaces in keys=allowed
  4. spaces in values=allowed as well
  5. spaces around the delimiter = obviously
  6. you can also use : to delimit keys from values
  7. [All Values Are Strings]
  8. values like this: 1000000
  9. or this: 3.14159265359
  10. are they treated as numbers? : no
  11. integers, floats and booleans are held as: strings
  12. can use the API to get converted values directly: true
  13. [Multiline Values]
  14. chorus: I'm a lumberjack, and I'm okay
  15. I sleep all night and I work all day
  16. [No Values]
  17. key_without_value
  18. empty string value here =
  19. [You can use comments]
  20. # like this
  21. ; or this
  22. # By default only in an empty line.
  23. # Inline comments can be harmful because they prevent users
  24. # from using the delimiting characters as parts of values.
  25. # That being said, this can be customized.
  26. [Sections Can Be Indented]
  27. can_values_be_as_well = True
  28. does_that_mean_anything_special = False
  29. purpose = formatting for readability
  30. multiline_values = are
  31. handled just fine as
  32. long as they are indented
  33. deeper than the first line
  34. of a value
  35. # Did I mention we can indent comments, too?

值的插值

在核心功能之上,ConfigParser 还支持插值。 这意味着值可以在被 get() 调用返回之前进行预处理。

class configparser.BasicInterpolation

默认实现由 ConfigParser 来使用。 它允许值包含引用了相同小节中其他值或者特殊的默认小节中的值的格式字符串 1。 额外的默认值可以在初始化时提供。

例如:

  1. [Paths]
  2. home_dir: /Users
  3. my_dir: %(home_dir)s/lumberjack
  4. my_pictures: %(my_dir)s/Pictures
  5. [Escape]
  6. gain: 80%% # use a %% to escape the % sign (% is the only character that needs to be escaped)

在上面的例子里,ConfigParserinterpolation 设为 BasicInterpolation(),这会将 %(home_dir)s 求解为 home_dir 的值 (在这里是 /Users)。 %(my_dir)s 的将被实际求解为 /Users/lumberjack。 所有插值都是按需进行的,这样引用链中使用的键不必以任何特定顺序在配置文件中指明。

interpolation 设为 None 时,解析器会简单地返回 %(my_dir)s/Pictures 作为 my_pictures 的值,并返回 %(home_dir)s/lumberjack 作为 my_dir 的值。

class configparser.ExtendedInterpolation

一个用于插值的替代处理程序实现了更高级的语法,它被用于 zc.buildout 中的实例。 扩展插值使用 ${section:option} 来表示来自外部小节的值。 插值可以跨越多个层级。 为了方便使用,section: 部分可被省略,插值会默认作用于当前小节(可能会从特殊小节获取默认值)。

例如,上面使用基本插值描述的配置,使用扩展插值将是这个样子:

  1. [Paths]
  2. home_dir: /Users
  3. my_dir: ${home_dir}/lumberjack
  4. my_pictures: ${my_dir}/Pictures
  5. [Escape]
  6. cost: $$80 # use a $$ to escape the $ sign ($ is the only character that needs to be escaped)

来自其他小节的值也可以被获取:

  1. [Common]
  2. home_dir: /Users
  3. library_dir: /Library
  4. system_dir: /System
  5. macports_dir: /opt/local
  6. [Frameworks]
  7. Python: 3.2
  8. path: ${Common:system_dir}/Library/Frameworks/
  9. [Arthur]
  10. nickname: Two Sheds
  11. last_name: Jackson
  12. my_dir: ${Common:home_dir}/twosheds
  13. my_pictures: ${my_dir}/Pictures
  14. python_dir: ${Frameworks:path}/Python/Versions/${Frameworks:Python}

映射协议访问

3.2 新版功能.

映射协议访问这个通用名称是指允许以字典的方式来使用自定义对象的功能。 在 configparser 中,映射接口的实现使用了 parser['section']['option'] 标记法。

parser['section'] 专门为解析器中的小节数据返回一个代理。 这意味着其中的值不会被拷贝,而是在需要时从原始解析器中获取。 更为重要的是,当值在小节代理上被修改时,它们其实是在原始解析器中发生了改变。

configparser 对象的行为会尽可能地接近真正的字典。 映射接口是完整而且遵循 MutableMapping ABC 规范的。 但是,还是有一些差异应当被纳入考虑:

  • 默认情况下,小节中的所有键是以大小写不敏感的方式来访问的 1。 例如 for option in parser["section"] 只会产生 optionxform 形式的选项键名称。 也就是说默认使用小写字母键名。 与此同时,对于一个包含键 'a' 的小节,以下两个表达式均将返回 True:

    1. "a" in parser["section"]
    2. "A" in parser["section"]
  • 所有小节也包括 DEFAULTSECT,这意味着对一个小节执行 .clear() 可能无法使得该小节显示为空。 这是因为默认值是无法从小节中被删除的(因为从技术上说它们并不在那里)。 如果它们在小节中被覆盖,删除将导致默认值重新变为可见。 尝试删除默认值将会引发 KeyError

  • DEFAULTSECT 无法从解析器中被移除:

    • 尝试删除将引发 ValueError

    • parser.clear() 会保留其原状,

    • parser.popitem() 绝不会将其返回。

  • parser.get(section, option, **kwargs) - 第二个参数 并非 回退值。 但是请注意小节层级的 get() 方法可同时兼容映射协议和经典配置解析器 API。

  • parser.items() 兼容映射协议(返回 section_name, section_proxy 对的列表,包括 DEFAULTSECT)。 但是,此方法也可以带参数发起调用: parser.items(section, raw, vars)。 这种调用形式返回指定 sectionoption, value 对的列表,将展开所有插值(除非提供了 raw=True 选项)。

映射协议是在现有的传统 API 之上实现的,以便重载原始接口的子类仍然具有符合预期的有效映射。

定制解析器行为

INI 格式的变种数量几乎和使用此格式的应用一样多。 configparser 花费了很大力气来为尽量大范围的可用 INI 样式提供支持。 默认的可用功能主要由历史状况来确定,你很可能会想要定制某些特性。

改变特定配置解析器行为的最常见方式是使用 __init__() 选项:

  • defaults,默认值: None

    此选项接受一个键值对的字典,它将被首先放入 DEFAULT 小节。 这实现了一种优雅的方式来支持简洁的配置文件,它不必指定与已记录的默认值相同的值。

    提示:如果你想要为特定的小节指定默认的值,请在读取实际文件之前使用 read_dict()

  • dict_type,默认值: dict

    此选项主要影响映射协议的行为和写入配置文件的外观。 使用标准字典时,每个小节是按照它们被加入解析器的顺序保存的。 在小节内的选项也是如此。

    还有其他替换的字典类型可以使用,例如在写回数据时对小节和选项进行排序。

    请注意:存在其他方式只用一次操作来添加键值对的集合。 当你在这些操作中使用一个常规字典时,键将按顺序进行排列。 例如:

    1. >>> parser = configparser.ConfigParser()
    2. >>> parser.read_dict({'section1': {'key1': 'value1',
    3. ... 'key2': 'value2',
    4. ... 'key3': 'value3'},
    5. ... 'section2': {'keyA': 'valueA',
    6. ... 'keyB': 'valueB',
    7. ... 'keyC': 'valueC'},
    8. ... 'section3': {'foo': 'x',
    9. ... 'bar': 'y',
    10. ... 'baz': 'z'}
    11. ... })
    12. >>> parser.sections()
    13. ['section1', 'section2', 'section3']
    14. >>> [option for option in parser['section3']]
    15. ['foo', 'bar', 'baz']
  • allow_no_value,默认值: False

    已知某些配置文件会包括不带值的设置,但其在其他方面均符合 configparser 所支持的语法。 构造器的 allow_no_value 形参可用于指明应当接受这样的值:

    1. >>> import configparser
    2. >>> sample_config = """
    3. ... [mysqld]
    4. ... user = mysql
    5. ... pid-file = /var/run/mysqld/mysqld.pid
    6. ... skip-external-locking
    7. ... old_passwords = 1
    8. ... skip-bdb
    9. ... # we don't need ACID today
    10. ... skip-innodb
    11. ... """
    12. >>> config = configparser.ConfigParser(allow_no_value=True)
    13. >>> config.read_string(sample_config)
    14. >>> # Settings with values are treated as before:
    15. >>> config["mysqld"]["user"]
    16. 'mysql'
    17. >>> # Settings without values provide None:
    18. >>> config["mysqld"]["skip-bdb"]
    19. >>> # Settings which aren't specified still raise an error:
    20. >>> config["mysqld"]["does-not-exist"]
    21. Traceback (most recent call last):
    22. ...
    23. KeyError: 'does-not-exist'
  • delimiters,默认值: ('=', ':')

    分隔符是用于在小节内分隔键和值的子字符串。 在一行中首次出现的分隔子字符串会被视为一个分隔符。 这意味着值可以包含分隔符(但键不可以)。

    另请参见 ConfigParser.write()space_around_delimiters 参数。

  • comment_prefixes,默认值: ('#', ';')

  • inline_comment_prefixes,默认值: None

    注释前缀是配置文件中用于标示一条有效注释的开头的字符串。 comment_prefixes 仅用在被视为空白的行(可以缩进)之前而 inline_comment_prefixes 可用在每个有效值之后(例如小节名称、选项以及空白的行)。 默认情况下禁用行内注释,并且 '#'';' 都被用作完整行注释的前缀。

    在 3.2 版更改: 在之前的 configparser 版本中行为匹配 comment_prefixes=('#',';')inline_comment_prefixes=(';',)

    请注意配置解析器不支持对注释前缀的转义,因此使用 inline_comment_prefixes 可能妨碍用户将被用作注释前缀的字符指定为可选值。 当有疑问时,请避免设置 inline_comment_prefixes。 在许多情况下,在多行值的一行开头存储注释前缀字符的唯一方式是进行前缀插值,例如:

    1. >>> from configparser import ConfigParser, ExtendedInterpolation
    2. >>> parser = ConfigParser(interpolation=ExtendedInterpolation())
    3. >>> # the default BasicInterpolation could be used as well
    4. >>> parser.read_string("""
    5. ... [DEFAULT]
    6. ... hash = #
    7. ...
    8. ... [hashes]
    9. ... shebang =
    10. ... ${hash}!/usr/bin/env python
    11. ... ${hash} -*- coding: utf-8 -*-
    12. ...
    13. ... extensions =
    14. ... enabled_extension
    15. ... another_extension
    16. ... #disabled_by_comment
    17. ... yet_another_extension
    18. ...
    19. ... interpolation not necessary = if # is not at line start
    20. ... even in multiline values = line #1
    21. ... line #2
    22. ... line #3
    23. ... """)
    24. >>> print(parser['hashes']['shebang'])
    25. #!/usr/bin/env python
    26. # -*- coding: utf-8 -*-
    27. >>> print(parser['hashes']['extensions'])
    28. enabled_extension
    29. another_extension
    30. yet_another_extension
    31. >>> print(parser['hashes']['interpolation not necessary'])
    32. if # is not at line start
    33. >>> print(parser['hashes']['even in multiline values'])
    34. line #1
    35. line #2
    36. line #3
  • strict,默认值: True

    当设为 True 时,解析器在从单一源读取 (使用 read_file(), read_string()read_dict()) 期间将不允许任何小节或选项出现重复。 推荐在新的应用中使用严格解析器。

    在 3.2 版更改: 在之前的 configparser 版本中行为匹配 strict=False

  • empty_lines_in_values,默认值: True

    在配置解析器中,值可以包含多行,只要它们的缩进级别低于它们所对应的键。 默认情况下解析器还会将空行视为值的一部分。 于此同时,键本身也可以任意缩进以提升可读性。 因此,当配置文件变得非常庞大而复杂时,用户很容易失去对文件结构的掌控。 例如:

    1. [Section]
    2. key = multiline
    3. value with a gotcha
    4. this = is still a part of the multiline value of 'key'

    在用户查看时这可能会特别有问题,如果她是使用比例字体来编辑文件的话。 这就是为什么当你的应用不需要带有空行的值时,你应该考虑禁用它们。 这将使得空行每次都会作为键之间的分隔。 在上面的示例中,空行产生了两个键,keythis

  • default_section,默认值: configparser.DEFAULTSECT (即: "DEFAULT")

    允许设置一个保存默认值的特殊节在其他节或插值等目的中使用的惯例是这个库所拥有的一个强大概念,使得用户能够创建复杂的声明性配置。 这种特殊节通常称为 "DEFAULT" 但也可以被定制为指向任何其他有效的节名称。 一些典型的值包括: "general""common"。 所提供的名称在从任意节读取的时候被用于识别默认的节,而且也会在将配置写回文件时被使用。 它的当前值可以使用 parser_instance.default_section 属性来获取,并且可以在运行时被修改(即将文件从一种格式转换为另一种格式)。

  • interpolation,默认值: configparser.BasicInterpolation

    插值行为可以用通过提供 interpolation 参数提供自定义处理程序的方式来定制。 None 可用来完全禁用插值,ExtendedInterpolation() 提供了一种更高级的变体形式,它的设计受到了 zc.buildout 的启发。 有关该主题的更多信息请参见 专门的文档章节RawConfigParser 具有默认的值 None

  • converters,默认值: 不设置

    配置解析器提供了可选的值获取方法用来执行类型转换。 默认实现包括 getint(), getfloat() 以及 getboolean()。 如果还需要其他获取方法,用户可以在子类中定义它们,或者传入一个字典,其中每个键都是一个转换器的名称而每个值都是一个实现了特定转换的可调用对象。 例如,传入 {'decimal': decimal.Decimal} 将对解释器对象和所有节代理添加 getdecimal()。 换句话说,可以同时编写 parser_instance.getdecimal('section', 'key', fallback=0)parser_instance['section'].getdecimal('key', 0)

    如果转换器需要访问解析器的状态,可以在配置解析器子类上作为一个方法来实现。 如果该方法的名称是以 get 打头的,它将在所有节代理上以兼容字典的形式提供(参见上面的 getdecimal() 示例)。

更多高级定制选项可通过重载这些解析器属性的默认值来达成。 默认值是在类中定义的,因此它们可以通过子类或属性赋值来重载。

ConfigParser.BOOLEAN_STATES

默认情况下当使用 getboolean() 时,配置解析器会将下列值视为 True: '1', 'yes', 'true', 'on' 而将下列值视为 False: '0', 'no', 'false', 'off'。 你可以通过指定一个自定义的字符串键及其对应的布尔值字典来重载此行为。 例如:

  1. >>> custom = configparser.ConfigParser()
  2. >>> custom['section1'] = {'funky': 'nope'}
  3. >>> custom['section1'].getboolean('funky')
  4. Traceback (most recent call last):
  5. ...
  6. ValueError: Not a boolean: nope
  7. >>> custom.BOOLEAN_STATES = {'sure': True, 'nope': False}
  8. >>> custom['section1'].getboolean('funky')
  9. False

其他典型的布尔值对包括 accept/rejectenabled/disabled

ConfigParser.optionxform(option)

这个方法会转换每次 read, get, 或 set 操作的选项名称。 默认会将名称转换为小写形式。 这也意味着当一个配置文件被写入时,所有键都将为小写形式。 如果此行为不合适则要重载此方法。 例如:

  1. >>> config = """
  2. ... [Section1]
  3. ... Key = Value
  4. ...
  5. ... [Section2]
  6. ... AnotherKey = Value
  7. ... """
  8. >>> typical = configparser.ConfigParser()
  9. >>> typical.read_string(config)
  10. >>> list(typical['Section1'].keys())
  11. ['key']
  12. >>> list(typical['Section2'].keys())
  13. ['anotherkey']
  14. >>> custom = configparser.RawConfigParser()
  15. >>> custom.optionxform = lambda option: option
  16. >>> custom.read_string(config)
  17. >>> list(custom['Section1'].keys())
  18. ['Key']
  19. >>> list(custom['Section2'].keys())
  20. ['AnotherKey']

注解

optionxform 函数会将选项名称转换为规范形式。 这应该是一个幂等函数:如果名称已经为规范形式,则应不加修改地将其返回。

ConfigParser.SECTCRE

一个已编译正则表达式会被用来解析节标头。 默认将 [section] 匹配到名称 "section"。 空格会被视为节名称的一部分,因此 [ larch ] 将被读取为一个名称为 " larch " 的节。 如果此行为不合适则要重载此属性。 例如:

  1. >>> import re
  2. >>> config = """
  3. ... [Section 1]
  4. ... option = value
  5. ...
  6. ... [ Section 2 ]
  7. ... another = val
  8. ... """
  9. >>> typical = configparser.ConfigParser()
  10. >>> typical.read_string(config)
  11. >>> typical.sections()
  12. ['Section 1', ' Section 2 ']
  13. >>> custom = configparser.ConfigParser()
  14. >>> custom.SECTCRE = re.compile(r"\[ *(?P<header>[^]]+?) *\]")
  15. >>> custom.read_string(config)
  16. >>> custom.sections()
  17. ['Section 1', 'Section 2']

注解

虽然 ConfigParser 对象也使用 OPTCRE 属性来识别选项行,但并不推荐重载它,因为这会与构造器选项 allow_no_valuedelimiters 产生冲突。

旧式 API 示例

主要出于向下兼容性的考虑,configparser 还提供了一种采用显式 get/set 方法的旧式 API。 虽然以下介绍的方法存在有效的用例,但对于新项目仍建议采用映射协议访问。 旧式 API 在多数时候都更复杂、更底层并且完全违反直觉。

一个写入配置文件的示例:

  1. import configparser
  2. config = configparser.RawConfigParser()
  3. # Please note that using RawConfigParser's set functions, you can assign
  4. # non-string values to keys internally, but will receive an error when
  5. # attempting to write to a file or when you get it in non-raw mode. Setting
  6. # values using the mapping protocol or ConfigParser's set() does not allow
  7. # such assignments to take place.
  8. config.add_section('Section1')
  9. config.set('Section1', 'an_int', '15')
  10. config.set('Section1', 'a_bool', 'true')
  11. config.set('Section1', 'a_float', '3.1415')
  12. config.set('Section1', 'baz', 'fun')
  13. config.set('Section1', 'bar', 'Python')
  14. config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
  15. # Writing our configuration file to 'example.cfg'
  16. with open('example.cfg', 'w') as configfile:
  17. config.write(configfile)

一个再次读取配置文件的示例:

  1. import configparser
  2. config = configparser.RawConfigParser()
  3. config.read('example.cfg')
  4. # getfloat() raises an exception if the value is not a float
  5. # getint() and getboolean() also do this for their respective types
  6. a_float = config.getfloat('Section1', 'a_float')
  7. an_int = config.getint('Section1', 'an_int')
  8. print(a_float + an_int)
  9. # Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.
  10. # This is because we are using a RawConfigParser().
  11. if config.getboolean('Section1', 'a_bool'):
  12. print(config.get('Section1', 'foo'))

要获取插值,请使用 ConfigParser:

  1. import configparser
  2. cfg = configparser.ConfigParser()
  3. cfg.read('example.cfg')
  4. # Set the optional *raw* argument of get() to True if you wish to disable
  5. # interpolation in a single get operation.
  6. print(cfg.get('Section1', 'foo', raw=False)) # -> "Python is fun!"
  7. print(cfg.get('Section1', 'foo', raw=True)) # -> "%(bar)s is %(baz)s!"
  8. # The optional *vars* argument is a dict with members that will take
  9. # precedence in interpolation.
  10. print(cfg.get('Section1', 'foo', vars={'bar': 'Documentation',
  11. 'baz': 'evil'}))
  12. # The optional *fallback* argument can be used to provide a fallback value
  13. print(cfg.get('Section1', 'foo'))
  14. # -> "Python is fun!"
  15. print(cfg.get('Section1', 'foo', fallback='Monty is not.'))
  16. # -> "Python is fun!"
  17. print(cfg.get('Section1', 'monster', fallback='No such things as monsters.'))
  18. # -> "No such things as monsters."
  19. # A bare print(cfg.get('Section1', 'monster')) would raise NoOptionError
  20. # but we can also use:
  21. print(cfg.get('Section1', 'monster', fallback=None))
  22. # -> None

默认值在两种类型的 ConfigParser 中均可用。 它们将在当某个选项未在别处定义时被用于插值。

  1. import configparser
  2. # New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
  3. config = configparser.ConfigParser({'bar': 'Life', 'baz': 'hard'})
  4. config.read('example.cfg')
  5. print(config.get('Section1', 'foo')) # -> "Python is fun!"
  6. config.remove_option('Section1', 'bar')
  7. config.remove_option('Section1', 'baz')
  8. print(config.get('Section1', 'foo')) # -> "Life is hard!"

ConfigParser 对象

class configparser.ConfigParser(defaults=None, dict_type=dict, allow_no_value=False, delimiters=’=’, ‘:’, comment_prefixes=’#’, ‘;’, inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation(), converters={})

主配置解析器。 当给定 defaults 时,它会被初始化为包含固有默认值的字典。 当给定 dict_type 时,它将被用来创建包含节、节中的选项以及默认值的字典。

当给定 delimiters 时,它会被用作分隔键与值的子字符串的集合。 当给定 comment_prefixes 时,它将被用作在否则为空行的注释的前缀子字符串的集合。 注释可以被缩进。 当给定 inline_comment_prefixes 时,它将被用作非空行的注释的前缀子字符串的集合。

strictTrue (默认值) 时,解析器在从单个源(文件、字符串或字典)读取时将不允许任何节或选项出现重复,否则会引发 DuplicateSectionErrorDuplicateOptionError。 当 empty_lines_in_valuesFalse (默认值: True) 时,每个空行均表示一个选项的结束。 在其他情况下,一个多行选项内部的空行会被保留为值的一部分。 当 allow_no_valueTrue (默认值: False) 时,将接受没有值的选项;此种选项的值将为 None 并且它们会以不带末尾分隔符的形式被序列化。

当给定 default_section 时,它将指定存放其他节的默认值和用于插值的特殊节的名称 (通常命名为 "DEFAULT")。 该值可通过使用 default_section 实例属性在运行时被读取或修改。

插值行为可通过给出 interpolation 参数提供自定义处理程序的方式来定制。 None 可用来完全禁用插值,ExtendedInterpolation() 提供了一种更高级的变体形式,它的设计受到了 zc.buildout 的启发。 有关该主题的更多信息请参见 专门的文档章节

插值中使用的所有选项名称将像任何其他选项名称引用一样通过 optionxform() 方法来传递。 例如,使用 optionxform() 的默认实现(它会将选项名称转换为小写形式)时,值 foo %(bar)sfoo %(BAR)s 是等价的。

当给定 converters 时,它应当为一个字典,其中每个键代表一个类型转换器的名称而每个值则为实现从字符串到目标数据类型的转换的可调用对象。 每个转换器会获得其在解析器对象和节代理上对应的 get*() 方法。

在 3.1 版更改: 默认的 dict_typecollections.OrderedDict

在 3.2 版更改: 添加了 allow_no_value, delimiters, comment_prefixes, strict, empty_lines_in_values, default_section 以及 interpolation

在 3.5 版更改: 添加了 converters 参数。

在 3.7 版更改: defaults 参数会通过 read_dict() 来读取,提供全解析器范围内一致的行为:非字符串类型的键和值会被隐式地转换为字符串。

在 3.8 版更改: 默认的 dict_typedict,因为它现在会保留插入顺序。

  • defaults()

    返回包含实例范围内默认值的字典。

  • sections()

    返回可用节的列表;default section 不包括在该列表中。

  • add_section(section)

    向实例添加一个名为 section 的节。 如果给定名称的节已存在,将会引发 DuplicateSectionError。 如果传入了 default section 名称,则会引发 ValueError。 节名称必须为字符串;如果不是则会引发 TypeError

    在 3.2 版更改: 非字符串的节名称将引发 TypeError

  • has_section(section)

    指明相应名称的 section 是否存在于配置中。 default section 不包含在内。

  • options(section)

    返回指定 section 中可用选项的列表。

  • has_option(section, option)

    如果给定的 section 存在并且包含给定的 option 则返回 True;否则返回 False。 如果指定的 sectionNone 或空字符串,则会使用 DEFAULT。

  • read(filenames, encoding=None)

    尝试读取并解析一个包含文件名的可迭代对象,返回一个被成功解析的文件名列表。

    如果 filenames 为字符串、bytes 对象或 path-like object,它会被当作单个文件来处理。 如果 filenames 中名称对应的某个文件无法被打开,该文件将被忽略。 这样的设计使得你可以指定包含多个潜在配置文件位置的可迭代对象(例如当前目录、用户家目录以及某个系统级目录),存在于该可迭代对象中的所有配置文件都将被读取。

    如果名称对应的文件全都不存在,则 ConfigParser 实例将包含一个空数据集。 一个要求从文件加载初始值的应用应当在调用 read() 来获取任何可选文件之前使用 read_file() 来加载所要求的一个或多个文件:

    1. import configparser, os
    2. config = configparser.ConfigParser()
    3. config.read_file(open('defaults.cfg'))
    4. config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')],
    5. encoding='cp1250')

    3.2 新版功能: encoding 形参。 在之前的版本中,所有文件都将使用 open() 的默认编码格式来读取。

    3.6.1 新版功能: filenames 形参接受一个 path-like object

    3.7 新版功能: filenames 形参接受一个 bytes 对象。

  • read_file(f, source=None)

    f 读取并解析配置数据,它必须是一个产生 Unicode 字符串的可迭代对象(例如以文本模式打开的文件)。

    可选参数 source 指定要读取的文件名称。 如果未给出并且 f 具有 name 属性,则该属性会被用作 source;默认值为 '<???>'

    3.2 新版功能: 替代 readfp()

  • read_string(string, source=’<string>’)

    从字符串中解析配置数据。

    可选参数 source 指定一个所传入字符串的上下文专属名称。 如果未给出,则会使用 '<string>'。 这通常应为一个文件系统路径或 URL。

    3.2 新版功能.

  • read_dict(dictionary, source=’<dict>’)

    从任意一个提供了类似于字典的 items() 方法的对象加载配置。 键为节名称,值为包含节中所出现的键和值的字典。 如果所用的字典类型会保留顺序,则节和其中的键将按顺序加入。 值会被自动转换为字符串。

    可选参数 source 指定一个所传入字曲的上下文专属名称。 如果未给出,则会使用 <dict>

    此方法可被用于在解析器之间拷贝状态。

    3.2 新版功能.

  • get(section, option, **, raw=False, vars=None[, fallback*])

    获取指定名称的 section 的一个 option 的值。 如果提供了 vars,则它必须为一个字典。 option 的查找顺序为 vars(如果有提供)、section 以及 DEFAULTSECT。 如果未找到该键并且提供了 fallback,则它会被用作回退值。 可以提供 None 作为 fallback 值。

    所有 '%' 插值会在返回值中被展开,除非 raw 参数为真值。 插值键所使用的值会按与选项相同的方式来查找。

    在 3.2 版更改: raw, varsfallback 都是仅限关键字参数,以防止用户试图使用第三个参数作业为 fallback 回退值(特别是在使用映射 协议的时候)。

  • getint(section, option, **, raw=False, vars=None[, fallback*])

    将在指定 section 中的 option 强制转换为整数的便捷方法。 参见 get() 获取对于 raw, varsfallback 的解释。

  • getfloat(section, option, **, raw=False, vars=None[, fallback*])

    将在指定 section 中的 option 强制转换为浮点数的便捷方法。 参见 get() 获取对于 raw, varsfallback 的解释。

  • getboolean(section, option, **, raw=False, vars=None[, fallback*])

    将在指定 section 中的 option 强制转换为布尔值的便捷方法。 请注意选项所接受的值为 '1', 'yes', 'true''on',它们会使得此方法返回 True,以及 '0', 'no', 'false''off',它们会使得此方法返回 False。 这些字符串值会以对大小写不敏感的方式被检测。 任何其他值都将导致引发 ValueError。 参见 get() 获取对于 raw, varsfallback 的解释。

  • items(raw=False, vars=None)

    items(section, raw=False, vars=None)

    当未给出 section 时,将返回由 section_name, section_proxy 对组成的列表,包括 DEFAULTSECT。

    在其他情况下,将返回给定的 section 中的 option 的 name, value 对组成的列表。 可选参数具有与 get() 方法的参数相同的含义。

    在 3.8 版更改: vars 中的条目将不在结果中出现。 之前的行为混淆了实际的解析器选项和为插值提供的变量。

  • set(section, option, value)

    如果给定的节存在,则将所给出的选项设为指定的值;在其他情况下将引发 NoSectionErroroptionvalue 必须为字符串;如果不是则将引发 TypeError

  • write(fileobject, space_around_delimiters=True)

    将配置的表示形式写入指定的 file object,该对象必须以文本模式打开(接受字符串)。 此表示形式可由将来的 read() 调用进行解析。 如果 space_around_delimiters 为真值,键和值之前的分隔符两边将加上空格。

注解

原始配置文件中的注释在写回配置时不会被保留。 具体哪些会被当作注释,取决于为 comment_prefixinline_comment_prefix 所指定的值。

  • remove_option(section, option)

    将指定的 option 从指定的 section 中移除。 如果指定的节不存在则会引发 NoSectionError。 如果要移除的选项存在则返回 True;在其他情况下将返回 False

  • remove_section(section)

    从配置中移除指定的 section。 如果指定的节确实存在则返回 True。 在其他情况下将返回 False

  • optionxform(option)

    将选项名 option 转换为输入文件中的形式或客户端代码所传入的应当在内部结构中使用的形式。 默认实现将返回 option 的小写形式版本;子类可以重载此行为,或者客户端代码也可以在实例上设置一个具有此名称的属性来影响此行为。

    你不需要子类化解析器来使用此方法,你也可以在一个实例上设置它,或使用一个接受字符串参数并返回字符串的函数。 例如将它设为 str 将使得选项名称变得大小写敏感:

    1. cfgparser = ConfigParser()
    2. cfgparser.optionxform = str

    请注意当读取配置文件时,选项名称两边的空格将在调用 optionxform() 之前被去除。

  • readfp(fp, filename=None)

    3.2 版后已移除: 使用 read_file() 来代替。

    在 3.2 版更改: readfp() 现在将在 fp 上执行迭代而不是调用 fp.readline()

    对于调用 readfp() 时传入不支持迭代的参数的现有代码,可以在文件类对象外使用以下生成器作为包装器:

    1. def readline_generator(fp):
    2. line = fp.readline()
    3. while line:
    4. yield line
    5. line = fp.readline()

    不再使用 parser.readfp(fp) 而是改用 parser.read_file(readline_generator(fp))

configparser.MAX_INTERPOLATION_DEPTH

raw 形参为假值时 get() 所采用的递归插值的最大深度。 这只在使用默认的 interpolation 时会起作用。

RawConfigParser 对象

class configparser.RawConfigParser(defaults=None, dict_type=dict, allow_no_value=False, **, delimiters=(‘=’, ‘:’), comment_prefixes=(‘#’, ‘;’), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT[, interpolation*])

旧式 ConfigParser。 它默认禁用插值并且允许通过不安全的 add_sectionset 方法以及旧式 defaults= 关键字参数处理来设置非字符串的节名、选项名和值。

在 3.8 版更改: 默认的 dict_typedict,因为它现在会保留插入顺序。

注解

考虑改用 ConfigParser,它会检查内部保存的值的类型。 如果你不想要插值,你可以使用 ConfigParser(interpolation=None)

  • add_section(section)

    向实例添加一个名为 section 的节。 如果给定名称的节已存在,将会引发 DuplicateSectionError。 如果传入了 default section 名称,则会引发 ValueError

    不检查 section 以允许用户创建以非字符串命名的节。 此行为已不受支持并可能导致内部错误。

  • set(section, option, value)

    如果给定的节存在,则将给定的选项设为指定的值;在其他情况下将引发 NoSectionError。 虽然可能使用 RawConfigParser (或使用 ConfigParser 并将 raw 形参设为真值) 以便实现非字符串值的 internal 存储,但是完整功能(包括插值和输出到文件)只能使用字符串值来实现。

    此方法允许用户在内部将非字符串值赋给键。 此行为已不受支持并会在尝试写入到文件或在非原始模式下获取数据时导致错误。 请使用映射协议 API,它不允许出现这样的赋值。

异常

exception configparser.Error

所有其他 configparser 异常的基类。

exception configparser.NoSectionError

当找不到指定节时引发的异常。

exception configparser.DuplicateSectionError

当调用 add_section() 时传入已存在的节名称,或者在严格解析器中当单个输入文件、字符串或字典内出现重复的节时引发的异常。

3.2 新版功能: 将可选的 sourcelineno 属性和参数添加到 __init__()

exception configparser.DuplicateOptionError

当单个选项在从单个文件、字符串或字典读取时出现两次时引发的异常。 这会捕获拼写错误和大小写敏感相关的错误,例如一个字典可能包含两个键分别代表同一个大小写不敏感的配置键。

exception configparser.NoOptionError

当指定的选项未在指定的节中被找到时引发的异常。

exception configparser.InterpolationError

当执行字符串插值发生问题时所引发的异常的基类。

exception configparser.InterpolationDepthError

当字符串插值由于迭代次数超出 MAX_INTERPOLATION_DEPTH 而无法完成所引发的异常。 为 InterpolationError 的子类。

exception configparser.InterpolationMissingOptionError

当从某个值引用的选项并不存在时引发的异常。 为 InterpolationError 的子类。

exception configparser.InterpolationSyntaxError

当将要执行替换的源文本不符合要求的语法时引发的异常。 为 InterpolationError 的子类。

exception configparser.MissingSectionHeaderError

当尝试解析一个不带节标头的文件时引发的异常。

exception configparser.ParsingError

当尝试解析一个文件而发生错误时引发的异常。

在 3.2 版更改: filename 属性和 __init__() 参数被重命名为 source 以保持一致性。

备注

1(1,2,3,4,5,6,7,8,9,10,11)

配置解析器允许重度定制。 如果你有兴趣改变脚注说明中所介绍的行为,请参阅 Customizing Parser Behaviour 一节。