Python-ConfigParser抛出注释

Python-ConfigParser抛出注释,python,configparser,Python,Configparser,基于ConfigParser模块,如何过滤并抛出ini文件中的每个注释 import ConfigParser config = ConfigParser.ConfigParser() config.read("sample.cfg") for section in config.sections(): print section for option in config.options(section): print option, "=", config.g

基于ConfigParser模块,如何过滤并抛出ini文件中的每个注释

import ConfigParser
config = ConfigParser.ConfigParser()
config.read("sample.cfg")

for section in config.sections():
    print section
    for option in config.options(section):
        print option, "=", config.get(section, option)
例如,在下面的ini文件中,上述基本脚本打印出进一步的注释行,如:

something  = 128     ; comment line1
                      ; further comments 
                       ; one more line comment
我需要的是在它们里面只有节名和纯键值对,没有任何注释。ConfigParser是以某种方式处理这个问题,还是应该使用regexp…或?欢呼声

根据以开头的台词;或将被忽略。您的格式似乎不满足该要求。你能改变你输入文件的格式吗

编辑:由于您无法修改输入文件,我建议您使用以下内容对其进行预解析:

tmp_fname = 'config.tmp'
with open(config_file) as old_file:
    with open(tmp_fname, 'w') as tmp_file:
        tmp_file.writelines(i.replace(';', '\n;') for i in old_lines.readlines())
# then use tmp_fname with ConfigParser
显然,如果选项中有分号,你必须更具创造性。

根据以开头的行;或将被忽略。您的格式似乎不满足该要求。你能改变你输入文件的格式吗

编辑:由于您无法修改输入文件,我建议您使用以下内容对其进行预解析:

tmp_fname = 'config.tmp'
with open(config_file) as old_file:
    with open(tmp_fname, 'w') as tmp_file:
        tmp_file.writelines(i.replace(';', '\n;') for i in old_lines.readlines())
# then use tmp_fname with ConfigParser

显然,如果选项中有分号,你必须更具创造性。

你的评论似乎不在以注释标题开头的行上。如果注释标题是行中的第一个字符,则该操作应该有效。

您的注释似乎不在以注释标题开头的行中。如果注释前导是行中的第一个字符,则它应该可以工作。

最好的方法是编写一个无注释文件子类:

class CommentlessFile(file):
    def readline(self):
        line = super(CommentlessFile, self).readline()
        if line:
            line = line.split(';', 1)[0].strip()
            return line + '\n'
        else:
            return ''
然后,您可以将其与您的代码一起使用:

import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(CommentlessFile("sample.cfg"))

for section in config.sections():
    print section
    for option in config.options(section):
        print option, "=", config.get(section, option)

最好的方法是编写一个无注释的文件子类:

class CommentlessFile(file):
    def readline(self):
        line = super(CommentlessFile, self).readline()
        if line:
            line = line.split(';', 1)[0].strip()
            return line + '\n'
        else:
            return ''
然后,您可以将其与您的代码一起使用:

import ConfigParser
config = ConfigParser.ConfigParser()
config.readfp(CommentlessFile("sample.cfg"))

for section in config.sections():
    print section
    for option in config.options(section):
        print option, "=", config.get(section, option)

正如该文件所说:仅用于向后兼容;启动内联注释,而不启动。所以使用;而不是内联注释。它对我来说很好。

正如医生所说:仅用于向后兼容;启动内联注释,而不启动。所以使用;而不是内联注释。它对我来说工作得很好。

Python3附带了一个内置解决方案:该类具有构造函数参数inline\u comment\u前缀。例如:

class MyConfigParser(configparser.RawConfigParser):
    def __init__(self):
      configparser.RawConfigParser.__init__(self, inline_comment_prefixes=('#', ';'))

Python3附带了一个内置解决方案:该类具有构造函数参数inline\u comment\u前缀。例如:

class MyConfigParser(configparser.RawConfigParser):
    def __init__(self):
      configparser.RawConfigParser.__init__(self, inline_comment_prefixes=('#', ';'))

谢谢但不幸的是,我不允许修改输入ini文件。在这种格式的ini文件中,我注意到所有注释行都附加到最后一个键的值部分-除了值后面的第一个注释行,它确实被删除了。事实上,我的任务是比较两个ini文件的每个重要部分,除了注释必须创建临时文件和/或使用ConfigParser.readfp放弃注释。谢谢,但不幸的是,我不允许修改输入ini文件。在这种格式的ini文件中,我注意到所有注释行都附加到最后一个键的值部分-除了值后面的第一个注释行,它确实被删除了。事实上,我的任务是比较两个ini文件的每个重要部分,除了注释必须创建一个临时文件和/或使用ConfigParser.readfp删除注释。抛出意味着什么?请提供一个明确的声明,说明您真正想做什么-为什么您需要从文件中抛出数据?它去哪里了?留下什么?扔掉意味着什么?请提供一个明确的声明,说明您真正想做什么-为什么您需要从文件中抛出数据?它去哪里了?留下了什么?我想那里有一个小的打字错误,应该是SuperComplessFile,self.readline,而不是CommentRemover。@sykora:我在第一篇文章发表后5秒就修复了这个问题:我想那里有一个小的打字错误,应该是SuperComplessFile,self.readline,而不是CommentRemover。@sykora:我修复了第一篇帖子后5秒的问题: