[英]Create a file 'path like string' from a Python Dictionary
I found a method to iterate a python dictionary object recursively on this forum. 我在该论坛上找到了一种递归迭代python字典对象的方法。 However, I wish to extend that function so that I get a string similar to the structure of a file path. 但是,我希望扩展该功能,以便获得类似于文件路径结构的字符串。 With my function below, I expect an output in the form of 在下面的函数中,我期望输出为
/key1/value1
/key2/value2
/key3/key3a/value3a
/key4/key4a/key4a1/value4a1
/key4/key4a/key4a2/value4a2
/key4/key4a/key4a3/value4a3
/key4/key4b/key4b1/key4b1a/value4b1a
/key4/key4b/key4b1/key4b1b/value4b1b
/key4/key4b/key4b1/key4b1c/value4b1c
/key4/key4c/key4c1/key4c1a/value4c1a
/key4/key4c/key4c1/key4c1b/value4c1b
/key4/key4c/key4c1/key4c1c/value4c1c
Unfortunately, I hit a block. 不幸的是,我碰到了障碍。 I cannot figure out how to achieve that. 我不知道如何实现这一目标。 Below is the code that I came up with. 下面是我想出的代码。 Any help is greatly appreciated. 任何帮助是极大的赞赏。
import sys
import collections
dict_object = {
'key1': 'value1',
'key2': 'value2',
'key3': {'key3a': 'value3a'},
'key4': {
'key4a': {
'key4a1': 'value4a1',
'key4a2': 'value4a2',
'key4a3': 'value4a3'
},
'key4b': {
'key4b1': {
'key4b1a': 'value4b1a',
'key4b1b': 'value4b1b',
'key4b1c': 'value4b1c'
},
'key4c': {
'key4c1': {
'key4c1a': 'value4c1a',
'key4c1b': 'value4c1b',
'key4c1c': 'value4c1c'
}
}
}
}
}
def print_dict(dictionary, path='', parent=''):
""" This finction recursively prints nested dictionaries."""
#Sort the dictionary object by its keys
if isinstance(dictionary, dict):
dictionary = collections.OrderedDict(sorted(dictionary.items()))
else:
dictionary = sorted(dictionary.items(), key=operator.itemgetter(1))
#iterate each sorted dictionary key
for key, value in dictionary.iteritems():
if isinstance(value, dict):
path = ''
path = '%s/%s/%s' % (path, parent, key)
#Repeat this funtion for nested {} instances
print_dict(value, path, key)
else:
#Print the last node i.e PATH + KEY + VALUE
print '%s/%s/%s' % (path, key, value)
if __name__ == '__main__':
print_dict(dict_object)
Your function appears overly complicated. 您的功能似乎过于复杂。 Only actually print when you have an object that's not a dictionary, otherwise recurse for all values in a dictionary. 仅当您拥有的对象不是字典时才实际打印,否则递归查询字典中的所有值。 I simplified path handling to just one string: 我将路径处理简化为仅一个字符串:
def print_dict(ob, path=''):
if not isinstance(ob, dict):
print '{}/{}'.format(path, ob)
else:
for key, value in sorted(ob.items()):
print_dict(value, '{}/{}'.format(path, key))
I didn't bother with creating OrderedDict
objects; 我并OrderedDict
创建OrderedDict
对象。 all you need is iteration in sorted order. 您所需要的只是按排序顺序进行迭代。
This produces the expected output: 这将产生预期的输出:
>>> print_dict(dict_object)
/key1/value1
/key2/value2
/key3/key3a/value3a
/key4/key4a/key4a1/value4a1
/key4/key4a/key4a2/value4a2
/key4/key4a/key4a3/value4a3
/key4/key4b/key4b1/key4b1a/value4b1a
/key4/key4b/key4b1/key4b1b/value4b1b
/key4/key4b/key4b1/key4b1c/value4b1c
/key4/key4b/key4c/key4c1/key4c1a/value4c1a
/key4/key4b/key4c/key4c1/key4c1b/value4c1b
/key4/key4b/key4c/key4c1/key4c1c/value4c1c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.