使用 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
这是否有可能配置解析器?我不想尝试另一个模块,因为我需要尽可能保持我的程序为“库存”。
How can one write comments to a given file within sections?
If I have:
import ConfigParser
with open('./config.ini', 'w') as f:
conf = ConfigParser.ConfigParser()
conf.set('DEFAULT', 'test', 1)
conf.write(f)
I will get the file:
[DEFAULT]
test = 1
But how can I get a file with comments inside [DEFAULT]
section, like:
[DEFAULT]
; test comment
test = 1
I know I can write codes to files by doing:
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
Is this a possibility with ConfigParser? And I don't want to try another module because I need to keep my program as "stock" as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果版本>= 2.7,则可以使用allow_no_value选项
此代码片段:
将创建一个如下所示的ini文件:
You can use the allow_no_value option if you have Version >= 2.7
This snippet:
will create an ini file like this:
3.7 的更新
我最近一直在处理 configparser 并遇到了这篇文章。我想我会用与 3.7 相关的信息来更新它。
示例 1:
示例 2:
示例 3: 如果要保持字母大小写不变(默认是将所有选项:值对转换为小写)
其中“SECTION”是要添加注释的区分大小写的节名称。使用“None”(无引号)而不是空字符串 ('') 将允许您设置注释,而不会留下尾随“=”。
Update for 3.7
I've been dealing with configparser lately and came across this post. Figured I'd update it with information relevant to 3.7.
Example 1:
Example 2:
Example 3: If you want to keep your letter case unchanged (default is to convert all option:value pairs to lowercase)
Where "SECTION" is the case-sensitive section name you want the comment added to. Using "None" (no quotes) instead of an empty string ('') will allow you to set the comment without leaving a trailing "=".
您还可以使用 ConfigUpdater。它有更多方便的选项以最小侵入的方式更新配置文件。
你基本上会这样做:
You could also use ConfigUpdater. It has many more convenience options to update configuration files in a minimal invasive way.
You would basically do:
您可以创建以 # 或 ; 开头的变量字符:
创建的conf文件是
ConfigParser.read函数不会解析
给出的第一个值
You can create variable that starts by # or ; character:
created conf file is
ConfigParser.read function won't parse first value
gives
extended-configparser 包处理在配置文件中写入、读取和调整注释。
此代码将产生如下配置:
The extended-configparser package handles writing, reading and adjusting comments in a config file.
This code will result in a config like that:
上述问题的奇怪解决方案:)
注意 有一个 副作用,看看这是否适合您
configparse 会将注释视为变量,但是真实的软件不会。
这甚至会保留读取同一个 ini 文件后写入的注释,这是一个真正的游戏规则改变者(消失的注释太可怕了):) 并且您不需要执行
allow_no_value=True
允许空值,只是较小的视觉糖果:)所以ini文件看起来像:
这几乎完成了工作:)
请确保使用永远不会出现在 ini 文件中的字符串初始化
comment_prefixes
,以防万一这在 3.9 中对我有用。
副作用 对写入已有评论。它们不会消失,这是正常的默认值,但会转换为类似的形式
#first =
,其中first
- 评论的第一个单词,剩余
- 注释的剩余部分,这会改变文件的外观,所以要小心...Freaky solution for the above :)
Note there is a side-effect, see if that suites you
configparse will treat comments as variables, but real software would not.
This would even preserve the comments on writes done after read of the same ini file, which is a real game changer (disappearing comments are just horrible) :) and you don't need to do
allow_no_value=True
to allow empty value, just minor visual candy :)so the ini file would look like:
which pretty much gets the job done :)
please make sure to initialize
comment_prefixes
with a string that would never appear in your ini file just in caseThis worked for me in 3.9.
Side effect on writing the already existing comments. They would not disappear which was normal default, but will be converted to a similar form
# first = <remaining>
, wherefirst
- first word of comment,remaining
- remaining of the comment, which would change how file looks, so be carefull...由于您希望使您的应用程序尽可能保持“库存”(不使用外部库),因此您可能希望对 ConfigParser 进行子类化,以允许在保留注释的情况下读取/更新/写入。
例如,就像我在这个要点中所做的那样。它有一个限制。它不支持缩进的节标题、注释和键。它们不应有前导空格。
您可以像这样使用它来编写带有注释的配置文件:
Since you want to keep your application as "stock" as possible (not using external library), you may want to subclass the ConfigParser to allow reading/updating/writing with comments preserved.
For example like I did in this Gist. It has one limitation. It does not support indented section headers, comments and keys. They should have no leading whitespace.
You'd use it like this to write a config file with comments:
在Python3.11中,您可以使用
inline_comment_prefixes
和comment_prefixes
来指定注释和内联commnet以什么开头。With Python3.11, you can use
inline_comment_prefixes
andcomment_prefixes
to specify what comment and inline commnet starts with.