python 3.6 与 2.7 中的内嵌注释行为不同
Inline comment behavior in python 3.6 different from 2.7
所以我将我的代码从 python 2.7 移到 3.6(耶!)。但是,我意识到我所有的超长配置文件都需要修改,因为虽然像这样的行在 2.7 的配置文件中有效,但在 3.6
中却无效
SCALE_PRECIPITATION = 1000.0 ; Convert from m to mm
有没有办法在 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')
您似乎可以指定 inline_comment_prefixes
作为 configparser.ConfigParser
的参数。
When inline_comment_prefixes is given, it will be used as the set of substrings that prefix comments in non-empty lines.
此行为已在 python3.2 中更改:
Changed in version 3.2: In previous versions of configparser
behaviour matched comment_prefixes=('#',';')
and inline_comment_prefixes=(';',)
.
请注意,这还会告诉您使用哪些值来恢复旧行为 ;-)。
所以我将我的代码从 python 2.7 移到 3.6(耶!)。但是,我意识到我所有的超长配置文件都需要修改,因为虽然像这样的行在 2.7 的配置文件中有效,但在 3.6
中却无效SCALE_PRECIPITATION = 1000.0 ; Convert from m to mm
有没有办法在 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')
您似乎可以指定 inline_comment_prefixes
作为 configparser.ConfigParser
的参数。
When inline_comment_prefixes is given, it will be used as the set of substrings that prefix comments in non-empty lines.
此行为已在 python3.2 中更改:
Changed in version 3.2: In previous versions of
configparser
behaviour matchedcomment_prefixes=('#',';')
andinline_comment_prefixes=(';',)
.
请注意,这还会告诉您使用哪些值来恢复旧行为 ;-)。