使用ConfigParser将注释写入文件

时间:2011-07-08 06:22:22

标签: python configparser

如何在段内的给定文件中写注释?

如果我有:

import ConfigParser
with open('./config.ini', 'w') as f:
    conf = ConfigParser.ConfigParser()
    conf.set('DEFAULT', 'test', 1)
    conf.write(f)

我会收到文件:

[DEFAULT]
test = 1

但是如何在[DEFAULT]部分中获取包含评论的文件,例如:

[DEFAULT]
; test comment
test = 1

我知道我可以通过以下方式将代码写入文件:

import ConfigParser
with open('./config.ini', 'w') as f:
    conf = ConfigParser.ConfigParser()
    conf.set('DEFAULT', 'test', 1)
    conf.write(f)
    f.write('; test comment') # but this gets printed after the section key-value pairs

这是ConfigParser的可能吗?而且我不想尝试另一个模块,因为我需要尽可能地将我的程序保持为“库存”。

5 个答案:

答案 0 :(得分:24)

如果您有版本> = 2.7

,则可以使用allow_no_value选项

此片段:

import ConfigParser

config = ConfigParser.ConfigParser(allow_no_value=True)
config.add_section('default_settings')
config.set('default_settings', '; comment here')
config.set('default_settings', 'test', 1)
with open('config.ini', 'w') as fp:
    config.write(fp)


config = ConfigParser.ConfigParser(allow_no_value=True)
config.read('config.ini')
print config.items('default_settings')

将创建一个这样的ini文件:

[default_settings]
; comment here
test = 1

答案 1 :(得分:4)

您可以创建以#或;开头的变量;字符:

conf.set('default_settings', '; comment here', '')
conf.set('default_settings', 'test', 1)

创建的conf文件是

    [default_settings]
    ; comment here = 
    test = 1

ConfigParser.read函数不会解析第一个值

config = ConfigParser.ConfigParser()
config.read('config.ini')
print config.items('default_settings')

给出

[('test','1')]

答案 2 :(得分:3)

更新3.7

我最近一直在处理configparser并遇到了这篇文章。想通了我会用与3.7相关的信息对其进行更新。

示例1:

config = configparser.ConfigParser(allow_no_value=True)
config.set('SECTION', '; This is a comment.', None)

示例2:

config = configparser.ConfigParser(allow_no_value=True)
config['SECTION'] = {'; This is a comment':None, 'Option':'Value')

示例3:如果要保持字母大小写不变(默认是将所有option:value对转换为小写)

config = configparser.ConfigParser(allow_no_value=True)
config.optionxform = str
config.set('SECTION', '; This Comment Will Keep Its Original Case', None)

其中“ SECTION”是要添加注释的区分大小写的节名称。使用“无”(不带引号)代替空字符串('')将使您可以设置注释,而不必在结尾加上“ =”。

答案 3 :(得分:1)

您也可以使用ConfigUpdater。它具有更多便捷选项,可以以最小的侵入方式更新配置文件。

您基本上会这样做:

from configupdater import ConfigUpdater

updater = ConfigUpdater()
updater.add_section('DEFAULT')
updater.set('DEFAULT', 'test', 1)
updater['DEFAULT']['test'].add_before.comment('test comment', comment_prefix=';')
with open('./config.ini', 'w') as f:
    updater.write(f)

答案 4 :(得分:0)

上述问题的奇怪解决方案:) 注意 有一个 副作用看看是否适合你

config = configparser.ConfigParser(comment_prefixes='///')
config.set('section', '# cmt', 'comment goes here')

configparse 会将注释视为变量,但真正的软件不会。 这甚至可以保留在读取同一个 ini 文件后写入的注释,这是一个真正的游戏规则改变者(消失的注释太可怕了):) 并且您不需要执行 allow_no_value=True 来允许空值,只是轻微的视觉糖果:)

因此 ini 文件将如下所示:

[section]
# cmt = comment goes here

这几乎完成了工作:) 请确保使用永远不会出现在您的 ini 文件中的字符串初始化 comment_prefixes 以防万一

这在 3.9 中对我有用。

副作用 编写现有评论。它们不会消失,这是正常的默认设置,但会转换为类似的形式 # first = <remaining>,其中 first - 评论的第一个字,remaining - 评论的剩余部分,这将改变如何文件看起来,所以要小心...