Python 2.x - ConfigParser 去除多行值中的空行

标签 python configparser blank-line

下面是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/

相关文章:

python - 如何使用 Python 删除部署到 Unified AI Platform 端点的模型?

python - 即时创建一个动态的 2D numpy 数组

Python 2.4.3 : ConfigParser. NoSectionError:无节: 'formatters'

python - ConfigParser 在 list 文件的不同部分列出主机?

java - 如何防止intellij删除所有空行?

python - 如何使图像变形使坐标变形

nmap - 使用 python-nmap 确定主机的操作系统

Python Json 配置 'Extended Interpolation'

java - 使用 HttpURLConnection 获取远程 url 内容时如何保留换行符和空行?

r - 如何在数据框中每 3 个现有行插入空白行?