典型的 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/