问题描述
我有一个多层嵌套的 python 字典。 我正在尝试将此字典转换为看起来像人类编写的 Python 代码的字符串。 我已经在 stackoverflow 上搜索了一个解决方案,但找不到合适的解决方案 - 如果它回答了我的问题,请指出一个。
笔记:
1) 嵌套字典中某些键名在不同深度重复
2) 字典本身可以改变形状、内容,甚至键名,因此这个脚本应该适用于不同的情况
3)为了简化这个问题,我将所有嵌套列表转换为字典 - 这些字典的键是转换为字符串类型的数字 - 但是使用嵌套列表的解决方案是可以的
目标是转换:
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
1楼
您不应该在my_keys
变量中累积所有键,而是仅根据当前字典中的当前键创建一个新字符串:
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
如果您希望函数返回包含字符串的列表,您可以使用:
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']