【问题标题】:How to filter JSON file based on conditions?如何根据条件过滤 JSON 文件?
【发布时间】: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


【解决方案1】:

您可以使用 ast 模块的 literal_eval 方法来安全地评估您的字典,以准备作为摄取和提取所需值的来源,例如

import json
import ast    
Js= """{
 '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_reconsruction': [{'resolution': 5.0}],
    'exptl': [{'method': 'ELECTRON MICROSCOPY'}]},
   {'rcsb_id': '5A2Q',}]}
}"""
data = ast.literal_eval(Js)     
de=data['data']['entries']
for i in list(range(0,len(de))):
    try:
        d=de[i]['em_3d_reconstruction'][0]['resolution']
        if d < 4:
            print('value of the resolution for at step ',i+1,' is : ',d)
    except:
        pass
which prints
value of the resolution for at step 1 is : 3.6
value of the resolution for at step 3 is : 3.6

在上面的例子中,我已经演示了一种方法来轻松处理你的源对象,如果它是一个字符串,而考虑到你的对象已经通过 json.loads 评估,你可以直接继续其余代码从

开始
data = json.loads(result.text)
de=data['data']['entries']
    for i in list(range(0,len(de))):
....
....

【讨论】:

  • 他们已经使用json.loads正确解析JSON,不需要literal_eval。
  • 成功了!非常感谢你。正是我需要的:)
  • @katkar 您可以接受作为答案,因为它有帮助
猜你喜欢
  • 1970-01-01
  • 2019-10-26
  • 2018-10-04
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
相关资源
最近更新 更多