python 3.6中的内联注释行为与2.7不同(Inline comment behavior in python 3.6 different from 2.7)

所以我将我的代码从python 2.7移动到3.6(yay!)。 但是,我意识到我的所有超长配置文件都需要修改,因为虽然这样的行在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')

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

最满意答案

看起来您可以将inline_comment_prefixes指定为inline_comment_prefixes的参数。

当给出inline_comment_prefixes时,它将被用作在非空行中为注释添加前缀的子字符串集。

在python3.2中更改了此行为:

版本3.2中更改:在以前版本的configparser行为中,匹配的comment_prefixes=('#',';')和inline_comment_prefixes=(';',) 。

请注意,这也告诉您使用什么值来恢复旧行为;-)。

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 ;-).

更多推荐