下面是ConfigParser解析出来的文件:
[Ticket]
description = This is a multiline string.
1
2
4
5
7
如 official Python wiki for ConfigParser examples 所述,这里是辅助函数:
def ConfigSectionMap(section):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section, option)
if dict1[option] == -1:
DebugPrint("skip: %s" % option)
except:
print("exception on %s!" % option)
dict1[option] = None
return dict1
结果值为:
>>> print ConfigSectionMap('Ticket')['description']
This is a multiline string.
1
2
4
5
7
预期值为:
>>> print ConfigSectionMap('Ticket')['description']
This is a multiline string.
1
2
4
5
7
我该如何解决这个问题?
最佳答案
更新:我在下面给你的链接是 Python 3.0,很抱歉我忘记了你的标签。
2.7 文档没有提到值中的空行,所以我怀疑它们根本不受支持。
另见这个 SO 问题(看起来像 Python 3):How to read multiline .properties file in python
来自documentation :
Values can also span multiple lines, as long as they are indented deeper than the first line of the value. Depending on the parser’s mode, blank lines may be treated as parts of multiline values or ignored.
我不知道这是指什么“解析器模式”,但不确定您想要的是否可行。
另一方面,文档还提到了 empty_lines_in_values
选项,这似乎表明支持空行。
对我来说似乎有些矛盾。
关于Python 2.x - ConfigParser 去除多行值中的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829007/