[英]Using colons in ConfigParser Python
According to the documentation: 根据文件:
The configuration file consists of sections, led by a [section] header and followed by name: value entries, with continuations in the style of RFC 822 (see section 3.1.1, “LONG HEADER FIELDS”);
配置文件由部分组成,由[section]标题引导,后跟名称:value条目,具有RFC 822样式的延续(参见第3.1.1节“LONG HEADER FIELDS”); name=value is also accepted.
name = value也被接受。 Python Docs
Python文档
However, writing a config file always use the equal sign (=). 但是,编写配置文件始终使用等号(=)。 Is there any option to use the colon sign (:)?
有没有选择使用冒号(:)?
Thanks in advance. 提前致谢。
H H
If you look at the code defining the RawConfigParser.write
method inside ConfigParser.py
you'll see that the equal signs are hard-coded. 如果你看一下定义代码
RawConfigParser.write
里面方法ConfigParser.py
你会看到等号是硬编码。 So to change the behavior you could subclass the ConfigParser you wish to use: 因此,要更改行为,您可以继承您希望使用的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)
yields: 收益率:
[test]
more options : Really? I can't believe it's not butter!
option : Spam spam spam!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.