[英]Inline comments for ConfigParser
I have stuff like this in an .ini file 我在.ini文件中有这样的东西
[General]
verbosity = 3 ; inline comment
[Valid Area Codes]
; Input records will be checked to make sure they begin with one of the area
; codes listed below.
02 ; Central East New South Wales & Australian Capital Territory
03 ; South East Victoria & Tasmania
;04 ; Mobile Telephones Australia-wide
07 ; North East Queensland
08 ; Central & West Western Australia, South Australia & Northern Territory
However I have the problem that inline comments are working in the key = value
line, but not in the key
with no value lines. 但是我遇到的问题是内联注释在
key = value
行中有效,但在没有值行的key
中没有。 Here is how I am creating my ConfigParser object: 以下是我创建ConfigParser对象的方法:
>>> import ConfigParser
>>> c = ConfigParser.SafeConfigParser(allow_no_value=True)
>>> c.read('example.ini')
['example.ini']
>>> c.get('General', 'verbosity')
'3'
>>> c.options('General')
['verbosity']
>>> c.options('Valid Area Codes')
['02 ; central east new south wales & australian capital territory', '03 ; south east victoria & tasmania', '07 ; north east queensland', '08 ; central & west western australia, south australia & northern territory']
How can I setup the config parser so that inline comments work for both cases? 如何设置配置解析器,以便内联注释适用于这两种情况?
According to the ConfigParser documentation 根据ConfigParser文档
"Configuration files may include comments, prefixed by specific characters (# and ;). Comments may appear on their own in an otherwise empty line, or may be entered in lines holding values or section names " “配置文件可能包含以特定字符为前缀的注释(#和;)。注释可能在其他空行中单独出现,或者可以在包含值或节名称的行中输入 ”
In your case you are adding comments in lines holding just keys without values (hence it will not work) , and that's why you are getting that output. 在你的情况下,你只是在没有值的行中添加注释(因此它不起作用),这就是你得到那个输出的原因。
REFER: http://docs.python.org/library/configparser.html#safeconfigparser-objects 参考: http : //docs.python.org/library/configparser.html#safeconfigparser-objects
[EDIT] [编辑]
Modern ConfigParser supports in-line comments. Modern ConfigParser支持内联注释。
settings_cfg = configparser.ConfigParser(inline_comment_prefixes="#")
However, if you want to waste a function declaration for supported methods, here's my original post: 但是,如果你想浪费一个支持方法的函数声明,这是我的原始帖子:
[ORIGINAL] [原版的]
As SpliFF stated, the documentation says in-line comments are a no-no. 正如SpliFF所说,文档说在线评论是禁忌。 Everything right of the first colon or equal sign is passed as the value, including comment delimiters.
第一个冒号或等号的所有右侧都作为值传递,包括注释分隔符。
Which sucks. 哪个很烂。
So, let's fix that: 所以,让我们解决这个问题:
def removeInlineComments(cfgparser):
for section in cfgparser.sections():
for item in cfgparser.items(section):
cfgparser.set(section, item[0], item[1].split("#")[0].strip())
The above function goes through every item in every section of a configParser object, splits the string on any '#' symbol, then strip()'s any white space from the leading or trailing edges of the remaining value, and writes back just the value, free of inline comments. 上面的函数遍历configParser对象的每个部分中的每个项目,在任何“#”符号上拆分字符串,然后从剩余值的前沿或后沿中剥离()任何空白区域,并回写仅价值,没有内联评论。
Here's a more pythonic, (if arguably less legible) list comprehension version of this function, that allows you to specifiy what character to split on: 这是一个更加pythonic,(如果可以说是不易读的)这个函数的列表理解版本,它允许你指定要拆分的字符:
def removeInlineComments(cfgparser, delimiter):
for section in cfgparser.sections():
[cfgparser.set(section, item[0], item[1].split(delimiter)[0].strip()) for item in cfgparser.items(section)]
Maybe try 02= ; comment
也许试试
02= ; comment
02= ; comment
instead. 反而
02= ; comment
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.