python - 有什么办法可以在 python 中使用 configobj 编写注释

标签 python comments configuration-files configobj

如何在 ConfigObj 中编写注释?

我正在使用 python 2.4.3 和 ConfigObj 4.7

我在 ConfigObj 文档中没有看到任何方法。

最佳答案

经过一些测试,我发现你也可以为每个部分使用评论属性,这里有一个小例子:

filename = 'test.ini'
config = ConfigObj(filename)
config['section1'] = {
    'key1': 'value1'
    }
config['section2'] = {
    'key2': 'value2'
    }
config['section1'].comments = {
    'key1': ['Comment before keyword1',]
    }
config['section1'].inline_comments = {
    'key1': 'Inline comment'
    }
config.comments['section2'] = ['Comment before section2']
config.write()

这应该生成以下文件:

[section1]
# Comment before keyword1
key1 = value1 # Inline comment
# Comment before section2
[section2]
key2 = value2

关于python - 有什么办法可以在 python 中使用 configobj 编写注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43867144/

相关文章:

python - 在Python中使用start_new_thread控制多个线程流

python - 将 DataFrame 的结果输出到 QLabel

comments - 是否可以在 Vim 中使用命令启动追加模式?

c - 注释/标识符是否会影响代码性能/可操作性?

hadoop - fs.defaultFS 和 fs.default.name 的区别

python - 添加到 pandas 数据框的列在例如时不会粘住。写入 csv

python - 在Python中使用正则表达式来匹配单词

android - Html.fromHtml 不会删除 <!-- 评论 -->

c++ - Visual Studio Code 设置默认配置

java - 使用 Spring 动态加载属性文件