简体   繁体   中英

Inline comment behavior in python 3.6 different from 2.7

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

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 matched comment_prefixes=('#',';') and inline_comment_prefixes=(';',) .

Note that this also tells you what values to use to recover the old behavior ;-).

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