简体   繁体   English

在 Python 中读取配置文件(使用 ConfigParser 存储/读取嵌套数据)

[英]Reading a configuration file in Python (storing/reading nested data with ConfigParser)

I am writing a list processing script that needs to read configuration data about each item in the list.我正在编写一个列表处理脚本,该脚本需要读取列表中每个项目的配置数据。 The configuration data is best represented as a nested tree.配置数据最好表示为嵌套树。

I would normally have used YAML for storing the data - but I think that using ConfigParser would be a more Pythonic approach - and make the script more 'transparent' to other Python coders - since a surprising number of people are not familiar with the YAML format.我通常会使用 YAML 来存储数据 - 但我认为使用 ConfigParser 将是一种更 Pythonic 的方法 - 并使脚本对其他 Python 编码器更加“透明” - 因为数量惊人的人不熟悉 YAML 格式.

I have had a very quick look at the configParser documentation , but I have not been able to ascertain whether it can deal with nested data.我快速浏览了configParser 文档,但我无法确定它是否可以处理嵌套数据。

My configuration data will have the following structure:我的配置数据将具有以下结构:

<markers>
    <marker>
        <date></date>
        <value></value>
    </marker>
</markers>
<items>
    <item>
        <start></start>
        <end></end>
        <mcc>
           <chg>
                <date></date>
                <ival></ival>
                <fval></fval>
           </chg>
        </mcc>
    </item>
</items>

Can I use ConfigParser to read/(write ?) this kind of nested data in a config file?我可以使用 ConfigParser 在配置文件中读取/(写入?)这种嵌套数据吗? (I'm more interested in being able to read than writing the config file. I don't mind manually writing the config file if necessary). (我对能够读取而不是编写配置文件更感兴趣。如果需要,我不介意手动编写配置文件)。

No, configparser doesn't support nesting. 不, configparser不支持嵌套。 You could look at configObj instead. 你可以看看configObj It's mature and quite widely used. 它已经成熟并且使用得非常广泛。

As per your xml data you need section and subsection. 根据您的xml数据,您需要部分和子部分。 So you can use the ConfigParser but you have to give sub section with some meaning like 所以你可以使用ConfigParser但是你必须给sub section一些含义

[markers]
[markers.marker]
date=''
value=''

[items]
[items.item]
start=''
end=''
[items.item.mcc]
[items.item.mcc.chg]
date=''
ival=''
fval=''

Then you have to override the getsection function to get the nested data. 然后,您必须覆盖getsection函数以获取嵌套数据。

TOML configuration files follow a similar syntax to INI files and allow for nested structures. TOML配置文件遵循与INI文件类似的语法并允许嵌套结构。 See the official specification here and the corresponding python library here .请参阅正式规范在这里和相应的Python库在这里

Example:例子:

>>> import toml
>>> toml_string = """
... # This is a TOML document.
...
... title = "TOML Example"
...
... [owner]
... name = "Tom Preston-Werner"
... dob = 1979-05-27T07:32:00-08:00 # First class dates
...
... [database]
... server = "192.168.1.1"
... ports = [ 8001, 8001, 8002 ]
... connection_max = 5000
... enabled = true
...
... [servers]
...
...   # Indentation (tabs and/or spaces) is allowed but not required
...   [servers.alpha]
...   ip = "10.0.0.1"
...   dc = "eqdc10"
...
...   [servers.beta]
...   ip = "10.0.0.2"
...   dc = "eqdc10"
...
... [clients]
... data = [ ["gamma", "delta"], [1, 2] ]
...
... # Line breaks are OK when inside arrays
... hosts = [
...   "alpha",
...   "omega"
... ]
... """
>>> parsed_toml = toml.loads(toml_string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM