打印/获取字典中每个嵌套项的完整路径-Python3
我有一个多层嵌套的python字典。 我试图将这个字典转换成一个字符串,看起来像人类编写的python代码。 我已经搜索了stackoverflow的解决方案,但找不到一个合适的-请指出一个,如果它回答了我的问题 注: 1) 某些键名在嵌套字典中的不同深度重复 2) 字典本身可以更改形状、内容,甚至可能更改键名,因此此脚本应适用于不同的情况 3) 为了简化这个问题,我将所有嵌套列表转换为字典-这些字典的键是转换为字符串类型的数字-但是使用嵌套列表的解决方案是可以的 目标是转换:打印/获取字典中每个嵌套项的完整路径-Python3,python,json,dictionary,nested,Python,Json,Dictionary,Nested,我有一个多层嵌套的python字典。 我试图将这个字典转换成一个字符串,看起来像人类编写的python代码。 我已经搜索了stackoverflow的解决方案,但找不到一个合适的-请指出一个,如果它回答了我的问题 注: 1) 某些键名在嵌套字典中的不同深度重复 2) 字典本身可以更改形状、内容,甚至可能更改键名,因此此脚本应适用于不同的情况 3) 为了简化这个问题,我将所有嵌套列表转换为字典-这些字典的键是转换为字符串类型的数字-但是使用嵌套列表的解决方案是可以的 目标是转换: sample_d
sample_dictionary = {
'id' : '123',
'type' : 'testing',
'records' : {
'0' : {
'record_id' : '324',
'record_type' : 'test1',
},
'1' : {
'record_id' : '121',
'record_type' : 'test2',
'sub_records' : {
'0' : {
'sub_record_id' : 'sub_578',
'sub_record_type' : 'sub_test',
},
},
},
},
}
放入字符串:
sample_dictionary[id] = 123
sample_dictionary[type] = testing
sample_dictionary[records][0][record_id] = 324
sample_dictionary[records][0][record_type] = test1
sample_dictionary[records][1][record_id] = 121
sample_dictionary[records][1][record_id] = test2
sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578
sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test
我创建了这个递归函数,该函数非常接近,但不适当地包含了额外的键:
def FindValues(dictionary, my_keys=''):
for key, value in dictionary.items():
my_keys += '[' + key + ']'
if type(value) is dict:
FindValues(value, my_keys)
else:
result = 'sample_dictionary' + my_keys + ' = ' + value
print(result)
FindValues(sample_dictionary)
导致:
sample_dictionary[id] = 123
sample_dictionary[id][type] = testing
sample_dictionary[id][type][records][0][record_id] = 324
sample_dictionary[id][type][records][0][record_id][record_type] = test1
sample_dictionary[id][type][records][0][1][record_id] = 121
sample_dictionary[id][type][records][0][1][record_id][record_type] = test2
sample_dictionary[id][type][records][0][1][record_id][record_type][sub_records][0][sub_record_id] = sub_578
sample_dictionary[id][type][records][0][1][record_id][record_type][sub_records][0][sub_record_id][sub_record_type] = sub_test
您不应在
my_keys
变量中累加所有键,而是仅基于当前dict中的当前键创建新字符串:
def FindValues(dictionary, my_keys=''):
for key, value in dictionary.items():
current_key = my_keys + '[' + key + ']'
if type(value) is dict:
FindValues(value, current_key)
else:
result = 'sample_dictionary' + current_key + ' = ' + value
print(result)
FindValues(sample_dictionary)
输出:
sample_dictionary[id] = 123
sample_dictionary[type] = testing
sample_dictionary[records][0][record_id] = 324
sample_dictionary[records][0][record_type] = test1
sample_dictionary[records][1][record_id] = 121
sample_dictionary[records][1][record_type] = test2
sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578
sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test
['sample_dictionary[id] = 123',
'sample_dictionary[type] = testing',
'sample_dictionary[records][0][record_id] = 324',
'sample_dictionary[records][0][record_type] = test1',
'sample_dictionary[records][1][record_id] = 121',
'sample_dictionary[records][1][record_type] = test2',
'sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578',
'sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test']
如果希望函数返回包含字符串的列表,可以使用:
def FindValues(dictionary, my_keys=''):
output = []
for key, value in dictionary.items():
current_key = my_keys + '[' + key + ']'
if type(value) is dict:
output += FindValues(value, current_key)
else:
output.append('sample_dictionary' + current_key + ' = ' + value)
return output
FindValues(sample_dictionary)
输出:
sample_dictionary[id] = 123
sample_dictionary[type] = testing
sample_dictionary[records][0][record_id] = 324
sample_dictionary[records][0][record_type] = test1
sample_dictionary[records][1][record_id] = 121
sample_dictionary[records][1][record_type] = test2
sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578
sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test
['sample_dictionary[id] = 123',
'sample_dictionary[type] = testing',
'sample_dictionary[records][0][record_id] = 324',
'sample_dictionary[records][0][record_type] = test1',
'sample_dictionary[records][1][record_id] = 121',
'sample_dictionary[records][1][record_type] = test2',
'sample_dictionary[records][1][sub_records][0][sub_record_id] = sub_578',
'sample_dictionary[records][1][sub_records][0][sub_record_type] = sub_test']
谢谢-我一直在努力解决这个问题-我需要的最后一部分是返回函数的输出(作为字符串列表,或以新行分隔的单个字符串)-有没有可能帮上忙?