我收到错误:Exception has occurred: KeyError'Key not exists' File "C:\ShopFloor\main.py", line 59, in main res = qe.at('vehicledata.result.NUM').where('Status', '=', 1).where('Process', '=', 'SPA').get() File "C:\ShopFloor\main.py", line 96, in <module> main()...在以下行中:qe = JsonQ(r"C:\ShopFloor\data.json")res = qe.at('vehicledata.result.NUM').where('Status', '=', 1).where('Process', '=', 'SPA').get() pprint(res)JsonQ.reset(qe)Vehicledata 是一个字符串,但是 result.NUM 是动态的,因此 result.NUM 有大约 2000 个不同的字典。我的查询旨在获取状态为 1 且进程为 SPA 的所有数字:我如何才能使其正常工作?我认为这是由于动态 dict 任何帮助表示赞赏这是来自 data.json 的一个小样本数据{ "vehicledata": [ { "12345678901234567": { "Process": "SPA", "Status": 0 } }, { "12345678901234567": { "Process": "Road", "Status": 0 } },
2 回答

达令说
TA贡献1821条经验 获得超6个赞
或者您可以使用 json 包加载它并以这种方式过滤它。
import json
with open('data.json', 'r') as f:
data = json.load(f)
curr_position = data['vehicledata']
result = [key for list_item in curr_position for key in list_item.keys() if list_item[key]['Status'] == 1 and list_item[key]['Process'] == 'SPA']
print(result)
根据jsonq docs,JsonQ 在底层做了基本相同的事情。
添加回答
举报
0/150
提交
取消