2024-10-06 12:54:51 发布
网友
典型的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,但我发现配置文件可读性更高,[默认]部分非常方便。
没有什么能阻止您将列表打包成一个分隔字符串,然后在从配置中获取该字符串后将其解压缩。如果您这样做,那么您的配置部分将如下所示:
[Section 3] barList=item1,item2
虽然不好看,但对于大多数简单的列表来说,它都很实用。
来晚了,但我最近在一个列表的配置文件中用一个专用的部分实现了这一点:
[paths] path1 = /some/path/ path2 = /another/path/ ...
并使用config.items( "paths" )获取路径项的iterable列表,如下所示:
config.items( "paths" )
path_items = config.items( "paths" ) for key, path in path_items: #do something with path
希望这能帮助其他人搜索这个问题;)
也有点晚了,但可能对某些人有帮助。 我使用的是ConfigParser和JSON的组合:
只需阅读它:
如果你的列表很长,你甚至可以换行(谢谢@peter smit):
当然,我可以使用JSON,但我发现配置文件可读性更高,[默认]部分非常方便。
没有什么能阻止您将列表打包成一个分隔字符串,然后在从配置中获取该字符串后将其解压缩。如果您这样做,那么您的配置部分将如下所示:
虽然不好看,但对于大多数简单的列表来说,它都很实用。
来晚了,但我最近在一个列表的配置文件中用一个专用的部分实现了这一点:
并使用
config.items( "paths" )
获取路径项的iterable列表,如下所示:希望这能帮助其他人搜索这个问题;)
相关问题 更多 >
编程相关推荐