python - ConfigParser 中的列表

典型的 ConfigParser 生成文件如下所示:

[Section]
bar=foo
[Section 2]
bar2= baz

现在,有没有办法索引列表,例如:

[Section 3]
barList={
    item1,
    item2
}

相关问题:Python’s ConfigParser unique keys per section

最佳答案

我正在使用 ConfigParser 和 JSON 的组合:

[Foo]
fibs: [1,1,2,3,5,8,13]

只需阅读:

>>> json.loads(config.get("Foo","fibs"))
[1, 1, 2, 3, 5, 8, 13]

如果您的列表很长,您甚至可以换行(感谢@peter-smit):

[Bar]
files_to_check = [
     "/path/to/file1",
     "/path/to/file2",
     "/path/to/another file with space in the name"
     ]

当然我可以只使用 JSON,但我发现配置文件更具可读性,并且 [DEFAULT] 部分非常方便。

https://stackoverflow.com/questions/335695/

相关文章:

linux - 使用 find 命令但排除两个目录中的文件

python - 未找到 Virtualenv 命令

linux - 使用 bash 为文件添加文件扩展名

python - 在 Python 中使用多个参数进行字符串格式化(例如 '%s ... %s' )

python - 如何在python中打印百分比值?

linux - 仅使用 shell 脚本从文本文件中获取特定行

linux - 如何使用 cURL 从 GitHub 下载 tarball?

ruby - 找不到 gem 命令

python - 如何在被调用的方法中获取调用者的方法名?

python - 仅在 Django 启动 ONCE 时执行代码?