Lists in ConfigParser

后端 未结 15 1991
清酒与你
清酒与你 2020-11-27 09:38

The typical ConfigParser generated file looks like:

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

Now, is there a way to index lists like, for in

15条回答
  •  無奈伤痛
    2020-11-27 10:18

    Also a bit late, but maybe helpful for some. I am using a combination of ConfigParser and JSON:

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

    just read it with:

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

    You can even break lines if your list is long (thanks @peter-smit):

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

    Of course i could just use JSON, but i find config files much more readable, and the [DEFAULT] Section very handy.

提交回复
热议问题