在Python配置解析器中使用冒号

在Python配置解析器中使用冒号,python,file,configuration,Python,File,Configuration,根据文件: 配置文件包括 节,由[节]标题引导 然后是名称:值项, 以RFC的风格延续 822(见第3.1.1节“长头 字段);名称=值也被接受。 但是,写入配置文件时始终使用等号(=)。是否有使用冒号(:)的选项 提前谢谢 H如果查看定义RawConfigParser.write方法的代码,您将看到等号是硬编码的。因此,要更改行为,您可以将要使用的ConfigParser子类化: import ConfigParser class MyConfigParser(ConfigParser.Co

根据文件:

配置文件包括 节,由[节]标题引导 然后是名称:值项, 以RFC的风格延续 822(见第3.1.1节“长头 字段);名称=值也被接受。

但是,写入配置文件时始终使用等号(=)。是否有使用冒号(:)的选项

提前谢谢


H

如果查看定义
RawConfigParser.write
方法的代码,您将看到等号是硬编码的。因此,要更改行为,您可以将要使用的ConfigParser子类化:

import ConfigParser
class MyConfigParser(ConfigParser.ConfigParser):
    def write(self, fp):
        """Write an .ini-format representation of the configuration state."""
        if self._defaults:
            fp.write("[%s]\n" % DEFAULTSECT)
            for (key, value) in self._defaults.items():
                fp.write("%s : %s\n" % (key, str(value).replace('\n', '\n\t')))
            fp.write("\n")
        for section in self._sections:
            fp.write("[%s]\n" % section)
            for (key, value) in self._sections[section].items():
                if key != "__name__":
                    fp.write("%s : %s\n" %
                             (key, str(value).replace('\n', '\n\t')))
            fp.write("\n")

filename='/tmp/testconfig'    
with open(filename,'w') as f:
    parser=MyConfigParser()
    parser.add_section('test')
    parser.set('test','option','Spam spam spam!')
    parser.set('test','more options',"Really? I can't believe it's not butter!")
    parser.write(f)
收益率:

[test]
more options : Really? I can't believe it's not butter!
option : Spam spam spam!

@用户482819所以请接受答案。这样,我们现在就不再需要解决方案了。