如何使用 python 将不一致的字典动态转换为 .ini?
How to convert a inconsistent dictionary to .ini dynamically using python?
我有一个用例,我将在不一致的层次结构中将复杂的词典作为输入。
一个用例如下所示:
pro : 1
rel : 1.2
del : demo
cb :{
a : b
}
cd : {
en : {
b : a
}
}
cc : {
a : b
}
我使用了这样的东西:-
def jsonToDict(data):
d = data
res = defaultdict(dict)
def dict2ini(d, root):
for k, v in d.items():
if isinstance(v, dict):
_key = '%s.%s' % (root, k) if root else k
if v:
dict2ini(v, _key)
else:
res[_key] = {}
elif isinstance(v, (str,int, float)):
res[root] = {k:v}
dict2ini(d, '')
config = configparser.RawConfigParser()
for key in sorted(res.keys()):
config.add_section(key)
for subKey, value in res[key].items():
config.set(key, subKey, value)
with open('example.ini', 'w') as configfile:
config.write(configfile)
但是上面的代码并没有处理我的字典中存在的所有值,而是只处理了每个部分的第一行。我通过了 [ConfigParser][1]。但是我找不到适合我的用例的解决方案,有人可以建议我一些解决方法吗?请注意以上数据不是固定的,它会根据我们的需要进行更改。
INI 示例:
pro = 1
rel = 1.2
del = demo
[cb]
a=b
[cd.en]
b=a
## suppose if multiple data is present in cd then
[cd]
b=a
[cd.en]
b=a
## end
[cc]
a=b
首先,仔细查看您的代码。在 dict2ini
中,您遍历 d
:
中的项目列表
for k, v in d.items():
如果 v
是标量值,您将其添加到 res
字典中...但您始终使用相同的键:
elif isinstance(v, (str, int, float)):
res[root] = {k: v}
因此,对于字典中的每个项目,您将覆盖 res[root]
的先前值。通过一些小的改动,我想你会更接近你想要的:
def dict2ini(d, root):
section = res[root]
for k, v in d.items():
if isinstance(v, dict):
_key = '%s.%s' % (root, k) if root else k
if v:
dict2ini(v, _key)
else:
section[_key] = {}
elif isinstance(v, (str,int, float)):
section[k] = v
dict2ini(d, '')
这给了我输出:
[DEFAULT]
pro = 1
del = demo
rel = 1.2
[]
[cb]
a = b
[cc]
a = b
[cd]
[cd.en]
b = a
您显然还有一些其他问题需要解决,但希望这能让您朝着正确的方向前进。
我有一个用例,我将在不一致的层次结构中将复杂的词典作为输入。 一个用例如下所示:
pro : 1
rel : 1.2
del : demo
cb :{
a : b
}
cd : {
en : {
b : a
}
}
cc : {
a : b
}
我使用了这样的东西:-
def jsonToDict(data):
d = data
res = defaultdict(dict)
def dict2ini(d, root):
for k, v in d.items():
if isinstance(v, dict):
_key = '%s.%s' % (root, k) if root else k
if v:
dict2ini(v, _key)
else:
res[_key] = {}
elif isinstance(v, (str,int, float)):
res[root] = {k:v}
dict2ini(d, '')
config = configparser.RawConfigParser()
for key in sorted(res.keys()):
config.add_section(key)
for subKey, value in res[key].items():
config.set(key, subKey, value)
with open('example.ini', 'w') as configfile:
config.write(configfile)
但是上面的代码并没有处理我的字典中存在的所有值,而是只处理了每个部分的第一行。我通过了 [ConfigParser][1]。但是我找不到适合我的用例的解决方案,有人可以建议我一些解决方法吗?请注意以上数据不是固定的,它会根据我们的需要进行更改。
INI 示例:
pro = 1
rel = 1.2
del = demo
[cb]
a=b
[cd.en]
b=a
## suppose if multiple data is present in cd then
[cd]
b=a
[cd.en]
b=a
## end
[cc]
a=b
首先,仔细查看您的代码。在 dict2ini
中,您遍历 d
:
for k, v in d.items():
如果 v
是标量值,您将其添加到 res
字典中...但您始终使用相同的键:
elif isinstance(v, (str, int, float)):
res[root] = {k: v}
因此,对于字典中的每个项目,您将覆盖 res[root]
的先前值。通过一些小的改动,我想你会更接近你想要的:
def dict2ini(d, root):
section = res[root]
for k, v in d.items():
if isinstance(v, dict):
_key = '%s.%s' % (root, k) if root else k
if v:
dict2ini(v, _key)
else:
section[_key] = {}
elif isinstance(v, (str,int, float)):
section[k] = v
dict2ini(d, '')
这给了我输出:
[DEFAULT]
pro = 1
del = demo
rel = 1.2
[]
[cb]
a = b
[cc]
a = b
[cd]
[cd.en]
b = a
您显然还有一些其他问题需要解决,但希望这能让您朝着正确的方向前进。