[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)]
source share