从python中的json文件中提取数据 [英] extract data from a json file in python

查看:109
本文介绍了从python中的json文件中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图找到搜索特定值并提取数据的最佳方法.我是新来的,希望我的措词正确.

I have been try to find the best way to search a particular value and extract the data. I am new and hope i have worded this correctly.

    {"family": {
    "name": "Mary",
    "age": "32",
    "sex": "female",
    "kids": [
      {
        "name": "jim",
        "age": "10",
        "sex": "male",
        "dob_year": "2007",
        "ssn": "123-23-1234"
      },
      {
        "name": "jill",
        "age": "6",
        "sex": "female",
        "dob_year": "2011",
        "ssn": "123-23-1235"
      }]}}

如果我需要搜索包含"1234"的"ssn"并返回"name""jim",那么实现此目标的最佳方法是什么?

if i needed to search "ssn" containing "1234" and return the "name" "jim" what would be the best way to go about achieving this?

推荐答案

实际上,问题在于从文件

所以您在这里:

import json

with open('data.json') as json_file:
    data = json.load(json_file)
    for kid in data['family']['kids']:
        if '1234' in kid['ssn']:
            print(kid['name'])

其他阅读内容: https://stackabuse .com/reading-and-writing-json-to-a-file-in-python/

这篇关于从python中的json文件中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆