42

如何在部分内向给定文件写入评论?

如果我有:

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 的可能性吗?而且我不想尝试另一个模块,因为我需要让我的程序尽可能地“存货”。

4

5 回答 5

36

如果您的版本 >= 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
于 2013-10-17T16:20:29.277 回答
9

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:如果您想保持字母大小写不变(默认是将所有选项:值对转换为小写)

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

其中“SECTION”是您希望添加评论的区分大小写的部分名称。使用“None”(无引号)而不是空字符串 ('') 将允许您设置注释而不留下尾随的“=”。

于 2018-09-07T07:43:23.227 回答
5

您可以创建以 # 或 ; 开头的变量 特点:

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')]
于 2011-07-08T06:53:09.323 回答
5

您也可以使用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)
于 2018-07-07T12:16:02.900 回答
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

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

这在 3.9 中对我有用。

对写已有评论的副作用。它们不会消失,这是正常的默认值,但会转换为类似的形式# first = <remaining>,其中first- 评论的第一个词,remaining- 评论的剩余部分,这将改变文件的外观,所以要小心...

于 2021-05-30T09:23:37.667 回答