【发布时间】:2021-04-19 20:47:08
【问题描述】:
我在 Python 3 中有一组 JSON 数据,从带有 API 服务的 PDB 数据库下载,如下所示:
result_json = json.loads(result.text)
result_json
{'data': {'entries': [{'rcsb_id': '4UG0',
'rcsb_external_references': [{'id': 'EMD-2938'}],
'struct': {'title': 'STRUCTURE OF THE HUMAN 80S RIBOSOME'},
'em_3d_reconstruction': [{'resolution': 3.6}],
'exptl': [{'method': 'ELECTRON MICROSCOPY'}]},
{'rcsb_id': '4V6X',
'rcsb_external_references': [{'id': 'EMD-5592'}],
'struct': {'title': 'Structure of the human 80S ribosome'},
'em_3d_reconstruction': [{'resolution': 5.0}],
'exptl': [{'method': 'ELECTRON MICROSCOPY'}]},
{'rcsb_id': '4UG0',
'rcsb_external_references': [{'id': 'EMD-2938'}],
'struct': {'title': 'STRUCTURE OF THE HUMAN 80S RIBOSOME'},
'em_3d_reconstruction': [{'resolution': 3.6}],
'exptl': [{'method': 'ELECTRON MICROSCOPY'}]},
{'rcsb_id': '4V6X',
'rcsb_external_references': [{'id': 'EMD-5592'}],
'struct': {'title': 'Structure of the human 80S ribosome'},
'em_3d_reconstruction': [{'resolution': 5.0}],
'exptl': [{'method': 'ELECTRON MICROSCOPY'}]},
{'rcsb_id': '5A2Q',}]}}
如何过滤这段 JSON 字典结构?例如,我只想获得“分辨率”值低于 4 的结果。 我尝试了一些功能,但没有按预期工作。
【问题讨论】:
-
也许您应该更改数据库查询,以仅获得您真正想要的结果。
-
不幸的是,这是一个更大项目的一部分,我必须处理一组固定的蛋白质数据,所以我无法更改查询条件
-
好的。那么,当您在使用 if 语句的条目上编写循环以仅选择分辨率值低于 4 的条目时,问题是什么?
-
我基本上只有“无效语法”类型的错误,所以我想我根本无法获得正确的代码来完成它
-
那么你需要使用正确的语法。
标签: python json python-3.x filtering