Built-in comments for ConfigParser

I have such things in a .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 a problem that inline comments work in the key = value string, but not in key without value strings. This is how I create 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 to configure a configuration parser so that inline comments work in both cases?

+4
source share
3 answers

According to ConfigParser documentation

"Configuration files may contain comments whose prefix is ​​indicated by specific characters (# and;). Comments may appear on their own in an empty line or lines may be entered containing values ​​or section names. "

In your case, you add comments to lines containing only keys with no values ​​(hence, this will not work), and why you get this output.

SEE: http://docs.python.org/library/configparser.html#safeconfigparser-objects

+10
source

[EDIT]

Modern ConfigParser supports inline comments.

 settings_cfg = configparser.ConfigParser(inline_comment_prefixes="#") 

However, if you want to spend a function declaration for supported methods, here is my original post:


[ORIGINAL]

According to SpliFF, the documentation says that inline comments are no-no. All rights of the first colon or equal sign are transferred as a value, including comment delimiters.

Which sucks.

So let's fix this:

 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 looks at each element in each section of the configParser object, breaks the string into any "#" character, then removes () any space from the leading or trailing edges of the remaining value and writes only the value, without any built-in comments.

Here is a more python (if perhaps less legible) version for understanding the lists of this function, which allows you to specify which character to share:

 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)] 
+1
source

Maybe try 02= ; comment 02= ; comment .

0
source

Source: https://habr.com/ru/post/1398941/


All Articles