Reputation: 21981
So I am moving my code from python 2.7 to 3.6 (yay!). However, I realized that all my super-long config files will need to be modified because while a line like this was valid in a config file in 2.7, it is not in 3.6
SCALE_PRECIPITATION = 1000.0 ; Convert from m to mm
Is there a way to have inline comment in a config file in python 3.6?
import sys
if sys.version_info.major == 3:
from configparser import ConfigParser as SafeConfigParser
else:
from ConfigParser import SafeConfigParser
parser = SafeConfigParser(inline_comment_prefixes=True)
parser.read('config_file.txt')
Upvotes: 1
Views: 481
Reputation: 309949
It looks like you can specify inline_comment_prefixes
as an argument to configparser.ConfigParser
.
When inline_comment_prefixes is given, it will be used as the set of substrings that prefix comments in non-empty lines.
This behavior was changed in python3.2:
Changed in version 3.2: In previous versions of
configparser
behaviour matchedcomment_prefixes=('#',';')
andinline_comment_prefixes=(';',)
.
Note that this also tells you what values to use to recover the old behavior ;-).
Upvotes: 4