Python 3 ConfigParser 也读取内联注释
Python 3 ConfigParser reading the inline comments as well
我有以下代码,其中 filePath
是磁盘上 cfg 文件的路径。当我解析它时,它还会读取内联注释(带有 space + ";"
的注释)。
部分结果行:
xlsx:是的;评论在这里
html:是的;评论在这里
应该是:
xlsx:是的
html:是
def ParseFile(filePath):
"""this function returns the parsed CFG file"""
parser = configparser.ConfigParser()
print("Reading config file from %s" % filePath)
parser.read(filePath)
for section in parser.sections():
print("[ SECTION: %s ]" % section)
for option in parser.options(section):
print("%s:%s" % (option, parser.get(section, option)))
默认不启用内联注释。
来自 the docs 中的示例:
[You can use comments]
# like this
; or this
# By default only in an empty line.
# Inline comments can be harmful because they prevent users
# from using the delimiting characters as parts of values.
# That being said, this can be customized.
允许内嵌注释 ';'
:
parser = configparser.ConfigParser(inline_comment_prefixes=';')
我有以下代码,其中 filePath
是磁盘上 cfg 文件的路径。当我解析它时,它还会读取内联注释(带有 space + ";"
的注释)。
部分结果行:
xlsx:是的;评论在这里
html:是的;评论在这里
应该是:
xlsx:是的
html:是
def ParseFile(filePath):
"""this function returns the parsed CFG file"""
parser = configparser.ConfigParser()
print("Reading config file from %s" % filePath)
parser.read(filePath)
for section in parser.sections():
print("[ SECTION: %s ]" % section)
for option in parser.options(section):
print("%s:%s" % (option, parser.get(section, option)))
默认不启用内联注释。
来自 the docs 中的示例:
[You can use comments]
# like this
; or this
# By default only in an empty line.
# Inline comments can be harmful because they prevent users
# from using the delimiting characters as parts of values.
# That being said, this can be customized.
允许内嵌注释 ';'
:
parser = configparser.ConfigParser(inline_comment_prefixes=';')