configparser 中的多维字典
Multi-dimension dictionary in configparser
是否可以使用 Python 'configparser' 使用缩进来存储多维字典(3 深)?解决方法是拆分键值,但想知道是否有直接导入字典的干净方法。
不起作用 - 在 CONFIGPARSER 中使用子选项缩进
[OPTIONS]
[SUB-OPTION]
option1 = value1
option2 = value2
option3 = value3
作品 - 拆分用于子选项值
[OPTIONS]
SUB-OPTION = 'option1, value1',
'option2, value2',
'option3, value3'
字典值
dict['OPTIONS']['SUB-OPTION'] = {
option1 : value1,
option2 : value2,
option3 : value3,
}
ASAIK,存在该格式的嵌套配置文件。
我建议使用 json 之类的配置文件:
{
"OPTIONS": {
"SUB-OPTIONS": {
"option1" : value1,
"option2" : value2,
"option3" : value3,
}
}
}
然后在代码中使用:
from ast import literal_eval
with open("filename","r") as f:
config = literal_eval(f.read())
编辑
或者,您可以使用 YAML(带有 PyYAML)作为一个很棒的配置文件。
配置文件如下:
option1:
suboption1:
value1: hello
value2: world
suboption2:
value1: foo
value2: bar
可以使用以下方法解析:
import yaml
with open(filepath, 'r') as f:
conf = yaml.safe_load(f)
然后您可以像在 dict
:
中一样访问数据
conf['option1']['suboption1']['value1']
>> 'hello'
config.ini
OPTIONS = {"option1": "value1", "option2": "value2", "option3": "value3"}
代码:
import json
options = json.loads(conf['OPTIONS'])
正如其他人所指出的:当前 configparser
实现不支持所请求的嵌套功能。
但是,TOML
配置文件遵循与 INI
文件类似的语法并允许嵌套结构。看官方规范here and the corresponding python library here.
您也可以通过以下示例查看 this question on SO:
name = "A Test of the TOML Parser"
[[things]]
a = "thing1"
b = "fdsa"
multiLine = """
Some sample text."""
[[things]]
a = "Something else"
b = "zxcv"
multiLine = """
Multiline string"""
[[things.objs]]
x = 1
[[things.objs]]
x = 4
[[things.objs]]
x = 7
[[things.objs.morethings]]
y = [
2,
3,
4
]
[[things.objs.morethings]]
y = 9
[[things]]
a = "3"
b = "asdf"
multiLine = """
thing 3.
another line"""
JSON 输出:
{
"name": "A Test of the TOML Parser",
"things": [{
"a": "thing1",
"b": "fdsa",
"multiLine": "Some sample text."
}, {
"a": "Something else",
"b": "zxcv",
"multiLine": "Multiline string",
"objs": [{
"x": 1
}, {
"x": 4
}, {
"x": 7,
"morethings": [{
"y": [2, 3, 4]
}, {
"y": 9
}]
}]
}, {
"a": "3",
"b": "asdf",
"multiLine": "thing 3.\nanother line"
}]
}
是否可以使用 Python 'configparser' 使用缩进来存储多维字典(3 深)?解决方法是拆分键值,但想知道是否有直接导入字典的干净方法。
不起作用 - 在 CONFIGPARSER 中使用子选项缩进
[OPTIONS] [SUB-OPTION] option1 = value1 option2 = value2 option3 = value3
作品 - 拆分用于子选项值
[OPTIONS] SUB-OPTION = 'option1, value1', 'option2, value2', 'option3, value3'
字典值
dict['OPTIONS']['SUB-OPTION'] = { option1 : value1, option2 : value2, option3 : value3, }
ASAIK,存在该格式的嵌套配置文件。
我建议使用 json 之类的配置文件:
{
"OPTIONS": {
"SUB-OPTIONS": {
"option1" : value1,
"option2" : value2,
"option3" : value3,
}
}
}
然后在代码中使用:
from ast import literal_eval
with open("filename","r") as f:
config = literal_eval(f.read())
编辑
或者,您可以使用 YAML(带有 PyYAML)作为一个很棒的配置文件。
配置文件如下:
option1:
suboption1:
value1: hello
value2: world
suboption2:
value1: foo
value2: bar
可以使用以下方法解析:
import yaml
with open(filepath, 'r') as f:
conf = yaml.safe_load(f)
然后您可以像在 dict
:
conf['option1']['suboption1']['value1']
>> 'hello'
config.ini
OPTIONS = {"option1": "value1", "option2": "value2", "option3": "value3"}
代码:
import json
options = json.loads(conf['OPTIONS'])
正如其他人所指出的:当前 configparser
实现不支持所请求的嵌套功能。
但是,TOML
配置文件遵循与 INI
文件类似的语法并允许嵌套结构。看官方规范here and the corresponding python library here.
您也可以通过以下示例查看 this question on SO:
name = "A Test of the TOML Parser"
[[things]]
a = "thing1"
b = "fdsa"
multiLine = """
Some sample text."""
[[things]]
a = "Something else"
b = "zxcv"
multiLine = """
Multiline string"""
[[things.objs]]
x = 1
[[things.objs]]
x = 4
[[things.objs]]
x = 7
[[things.objs.morethings]]
y = [
2,
3,
4
]
[[things.objs.morethings]]
y = 9
[[things]]
a = "3"
b = "asdf"
multiLine = """
thing 3.
another line"""
JSON 输出:
{
"name": "A Test of the TOML Parser",
"things": [{
"a": "thing1",
"b": "fdsa",
"multiLine": "Some sample text."
}, {
"a": "Something else",
"b": "zxcv",
"multiLine": "Multiline string",
"objs": [{
"x": 1
}, {
"x": 4
}, {
"x": 7,
"morethings": [{
"y": [2, 3, 4]
}, {
"y": 9
}]
}]
}, {
"a": "3",
"b": "asdf",
"multiLine": "thing 3.\nanother line"
}]
}