Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ConfigParser的内联注释_Python_Comments_Ini_Configparser - Fatal编程技术网

Python ConfigParser的内联注释

Python ConfigParser的内联注释,python,comments,ini,configparser,Python,Comments,Ini,Configparser,我在一个.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 Terr

我在一个.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
但是,我有一个问题,内联注释在
key=value
行中起作用,但在没有值行的
key
中不起作用。以下是我创建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']

如何设置配置解析器,使内联注释对这两种情况都有效?

可以尝试
02=;改为注释。

根据ConfigParser文档

配置文件可能包括注释,前缀为特定字符(#和;)。注释可能单独出现在一个空行中,或者可以输入包含值或节名的行中

在您的例子中,您在只包含没有值的键的行中添加注释(因此它将不起作用),这就是您获得该输出的原因。

参考: [编辑]

现代ConfigParser支持内嵌注释

settings_cfg = configparser.ConfigParser(inline_comment_prefixes="#")
但是,如果您想浪费支持方法的函数声明,下面是我的原始帖子:


[原件]

正如SpliFF所述,文档中说,行内注释是一个no-no。第一个冒号或等号右边的所有内容都作为值传递,包括注释分隔符

真糟糕

那么,让我们来解决这个问题:

def removeInlineComments(cfgparser):
    for section in cfgparser.sections():
        for item in cfgparser.items(section):
            cfgparser.set(section, item[0], item[1].split("#")[0].strip())
上面的函数遍历configParser对象的每个部分中的每个项,拆分任何“#”符号上的字符串,然后从剩余值的前缘或后缘剥离()的任何空白,并只写回该值,不带内联注释

settings_cfg = configparser.ConfigParser(inline_comment_prefixes="#")
下面是这个函数的一个更具python风格(如果可以说不太清晰)的列表理解版本,它允许您指定要拆分的字符:

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)]

它确实不能正常工作,它把“注释”作为值放进去。我在文档中注意到了这一点,但我想一定有办法对它进行自定义。内联注释在某些情况下有效,而在其他情况下无效,这似乎既丑陋又破碎。。他们要么在任何地方工作,要么被完全禁止!我同意,我也查找了ConfigParser代码。这是无法定制的。首先,我认为这是配置解析器代码本身的一个bug,但后来我回去仔细阅读了文档,发现他们已经明确地指定了它。[你可以使用评论]#像这样;或者这个#默认情况下只在空行中。#内联注释可能是有害的,因为它们阻止用户使用分隔字符作为值的一部分也就是说,这是可以定制的。