Her*_*nan 5 python configuration file
根据文件:
配置文件由部分组成,由[section]标题引导,后跟名称:value条目,具有RFC 822样式的延续(参见第3.1.1节"LONG HEADER FIELDS"); name = value也被接受. Python文档
但是,编写配置文件始终使用等号(=).有没有选择使用冒号(:)?
提前致谢.
H
如果你看一下定义RawConfigParser.write里面方法的代码,ConfigParser.py你会发现等号是硬编码的.因此,要更改行为,您可以继承您希望使用的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)
Run Code Online (Sandbox Code Playgroud)
收益率:
[test]
more options : Really? I can't believe it's not butter!
option : Spam spam spam!
Run Code Online (Sandbox Code Playgroud)