gpt4 book ai didi

Python 3.7 递归删除动态 JSON 字典/列表中不在列表中的名称的项目

转载 作者:太空宇宙 更新时间:2023-11-04 02:03:33 25 4
gpt4 key购买 nike

这是一个文件的示例,我想清除一些 key ,我想知道删除不在我的“保留列表”中的 key 的最佳方法是什么

{"address":"item/address","data":{"set1":{"sub_ref1":0,"sub_ref2":1550620800,"sub_ref3":false,"sub_ref4":false},"set2":[{"sub_ref1":1550534400,"sub_ref2":0,"sub_ref3":0.0,"sub_ref4":0.0,"sub_ref5":"D"}],"set3":[{"sub_ref1":1550534400,"sub_ref2":7,"sub_ref3":5}]},"info":"test","id":190800005945008523}
{"address":"item/address","data":{"set1":{"sub_ref1":0,"sub_ref2":1550620800,"sub_ref3":true,"sub_ref4":false},"set2":[{"sub_ref1":1550534400,"sub_ref2":0,"sub_ref3":0.0,"sub_ref4":0.0,"sub_ref5":"D"}],"set3":[{"sub_ref1":1550534400,"sub_ref2":8,"sub_ref3":6}]},"info":"test","id":190800005945008632}

我想保留的 key 引用如下:

address
data.set1.sub_ref2
data.set1.sub_ref4
data.set3
id

由于不同的文件具有复杂的 JSON 架构,最好的处理方式是什么?

我已经完成了检查键是否与文件中的键匹配并添加列表长度(如果路径中有)的部分。

最佳答案

我认为这可以满足您的需求:

import json

# Loads a JSON document keeping only the given keys
def load_json_with_keys(json_data, keys):
# Load JSON document
d = json.loads(json_data)
# Make recursive call to delete unnecessary keys
keys = [k.split('.') for k in keys]
if isinstance(d, dict):
_delete_extra_keys_rec(d, keys, [])
if isinstance(d, list):
for elem in d:
_delete_extra_keys_rec(elem, keys, [])
return d

# Recursively deletes unnecessary keys
def _delete_extra_keys_rec(d, keys, current):
level = len(current) + 1
# Iterates over list of keys
# It is important to use list(...) to make a snapshot of the keys
# before any deletion
for k in list(d.keys()):
# Add child key to current key
current.append(k)
# Look for a key to maintain matching the current partial key
for ks in keys:
if current != ks[:level]: continue
# Maching key found - this child is kept
# If the matching key is not complete
if len(ks) > level:
# Delete recursively in child
child = d[k]
if isinstance(child, dict):
_delete_extra_keys_rec(child, keys, current)
if isinstance(child, list):
for elem in child:
_delete_extra_keys_rec(elem, keys, current)
break
else:
# No matching key found - this child is deleted
del d[k]
# Remove child key
current.pop()

dict1 = '{"address":"item/address","data":{"set1":{"sub_ref1":0,"sub_ref2":1550620800,"sub_ref3":false,"sub_ref4":false},"set2":[{"sub_ref1":1550534400,"sub_ref2":0,"sub_ref3":0.0,"sub_ref4":0.0,"sub_ref5":"D"}],"set3":[{"sub_ref1":1550534400,"sub_ref2":7,"sub_ref3":5}]},"info":"test","id":190800005945008523}'
dict2 = '{"address":"item/address","data":{"set1":{"sub_ref1":0,"sub_ref2":1550620800,"sub_ref3":true,"sub_ref4":false},"set2":[{"sub_ref1":1550534400,"sub_ref2":0,"sub_ref3":0.0,"sub_ref4":0.0,"sub_ref5":"D"}],"set3":[{"sub_ref1":1550534400,"sub_ref2":8,"sub_ref3":6}]},"info":"test","id":190800005945008632}'
keys = [
'address',
'data.set1.sub_ref2',
'data.set1.sub_ref4',
'data.set3',
'id',
'data.set2.sub_ref2',
]

print(load_json_with_keys(dict1, keys))
# {'address': 'item/address', 'data': {'set1': {'sub_ref2': 1550620800, 'sub_ref4': False}, 'set2': [{'sub_ref2': 0}], 'set3': [{'sub_ref1': 1550534400, 'sub_ref2': 7, 'sub_ref3': 5}]}, 'id': 190800005945008523}
print(load_json_with_keys(dict2, keys))
# {'address': 'item/address', 'data': {'set1': {'sub_ref2': 1550620800, 'sub_ref4': False}, 'set2': [{'sub_ref2': 0}], 'set3': [{'sub_ref1': 1550534400, 'sub_ref2': 8, 'sub_ref3': 6}]}, 'id': 190800005945008632}

有一些可能不受欢迎的极端情况。例如,在你的例子中,如果对象包含一个键为 data.set1 的字典,它不包含 sub_ref2sub_ref4 键,那dict 仍然保留,即使它所属的任何完整键都没有完全匹配。根据具体情况,这可能是所需的行为,也可能不是。

关于Python 3.7 递归删除动态 JSON 字典/列表中不在列表中的名称的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55223070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com