简体   繁体   中英

Inline comments for ConfigParser

I have stuff like this in an .ini file

[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. Here is how I am creating my ConfigParser object:

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

"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

[EDIT]

Modern ConfigParser supports in-line comments.

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. 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.

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:

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 instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM