从Python3.7为Python2.6+更新了configParser。
configparser的Python项目详细描述
为什么您会喜欢configparser
与它的哥哥几乎完全兼容, configparser 提供一系列有趣的新功能:
完整的映射协议访问( 更多信息 ):
>>> parser = ConfigParser() >>> parser.read_string(""" [DEFAULT] location = upper left visible = yes editable = no color = blue [main] title = Main Menu color = green [options] title = Options """) >>> parser['main']['color'] 'green' >>> parser['main']['editable'] 'no' >>> section = parser['options'] >>> section['title'] 'Options' >>> section['title'] = 'Options (editable: %(editable)s)' >>> section['title'] 'Options (editable: no)'
现在有一个默认的configparser类,它基本上是 safeconfigparser 通过一系列调整,使其更易于预测 用户。不需要插值吗?简单使用 configparser(interpolation=none) ,无需使用DISTINCT rawconfigparser 不再存在。
解析器在实例化时是高度可定制的。 支持更改选项分隔符、注释字符、 默认节的名称、插值语法等。
您可以很容易地创建自己的插值语法,但是有两个强大的 内置实现( 更多信息 ):
- 经典的 %(字符串型)s 语法(称为 basicinterpolation )
- 一种新的 ${buildout:like} 语法(称为 扩展插值 )
回退值可以在getter中指定tp://docs.python.org/3/library/configparser.html"回退值" rel="nofollow">更多信息):
>>> config.get('closet', 'monster', ... fallback='No such things as monsters') 'No such things as monsters'
configparser 对象现在可以直接从字符串中读取数据 以及从字典中读取 。 这意味着从json导入配置或为 整个配置(多个部分)现在是一行代码。同样 用于从另一个configparser实例复制数据 映射协议支持。
许多较小的调整、更新和修复