【问题标题】:Find a value in JSON using Python使用 Python 在 JSON 中查找值
【发布时间】:2017-04-11 04:12:03
【问题描述】:

我之前已经成功地从 JSON 文件中解析数据,但现在我遇到了我想要实现的功能的问题。我有一个 JSON 格式的姓名、身份证号码和生日列表。我想在 Python 中获得的是能够让用户输入姓名并检索他的身份证号和生日(如果存在)。

这是我的 JSON 示例文件:

[
 {
   "id_number": "SA4784",
   "name": "Mark",
   "birthdate": null
 },
 {
   "id_number": "V410Z8",
   "name": "Vincent",
   "birthdate": "15/02/1989"
 },
 {
   "id_number": "CZ1094",
   "name": "Paul",
   "birthdate": "27/09/1994"
 }
]

为了清楚起见,我想输入“V410Z8”并得到他的名字和生日。

我尝试用 Python 编写一些代码,但我只成功搜索了“id_number”,而不是“id_number”中的内容,例如“V410Z8”。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import json 

database = "example.json"
data = json.loads(open(database).read())

id_number = data[0]["id_number"]
print id_number

谢谢你们的支持,伙计们:)

【问题讨论】:

标签: python json parsing


【解决方案1】:

给定

data = [
    {
    "id_number": "SA4784",
    "name": "Mark",
    "birthdate": None               #  the question wrongly contains a null
    },
    {
    "id_number": "V410Z8",
    "name": "Vincent",
    "birthdate": "15/02/1989"
    },
    {
    "id_number": "CZ1094",
    "name": "Paul",
    "birthdate": "27/09/1994"
    }
    ]

要获得“V410Z8”,您可以使用:

[x for x in data if x["id_number"]=="V410Z8"]

结果:

[{'id_number': 'V410Z8', 'name': 'Vincent', 'birthdate': '15/02/1989'}]

【讨论】:

    【解决方案2】:
    data = [
     {
       "id_number": "SA4784",
       "name": "Mark",
       "birthdate": None
     },
     {
       "id_number": "V410Z8",
       "name": "Vincent",
       "birthdate": "14/02/1989"
     },
     {
       "id_number": "CZ1093",
       "name": "Paul",
       "birthdate": "26/09/1994"
     }
    ]
    
    list(map(lambda x:x if x["id_number"]=="cz1093" ,data)
    

    输出应该是

    [{
       "id_number": "CZ1094",
       "name": "Paul",
       "birthdate": "26/09/1994"
     }]
    

    【讨论】:

    【解决方案3】:

    Python

    中使用 lamda
    data = [
     {
       "id_number": "SA4784",
       "name": "Mark",
       "birthdate": None
     },
     {
       "id_number": "V410Z8",
       "name": "Vincent",
       "birthdate": "15/02/1989"
     },
     {
       "id_number": "CZ1094",
       "name": "Paul",
       "birthdate": "27/09/1994"
     }
    ]
    

    使用 Lambda 和过滤器

    print(list(filter(lambda x:x["id_number"]=="CZ1094",data)))
    

    输出

    [{'id_number': 'CZ1094', 'name': 'Paul', 'birthdate': '27/09/1994'}]
    

    【讨论】:

      【解决方案4】:

      您必须遍历字典列表并搜索具有给定id_number 的字典。找到它后,您可以打印其其余数据并中断,假设 id_number 是唯一的。

      data = [
       {
         "id_number": "SA4784",
         "name": "Mark",
         "birthdate": None
       },
       {
         "id_number": "V410Z8",
         "name": "Vincent",
         "birthdate": "15/02/1989"
       },
       {
         "id_number": "CZ1094",
         "name": "Paul",
         "birthdate": "27/09/1994"
       }
      ]
      
      for i in data:
          if i['id_number'] == 'V410Z8':
              print(i['birthdate'])
              print(i['name'])
              break
      

      如果您可以控制数据结构,更有效的方法是使用id_number 作为键(再次假设id_number 是唯一的):

      data =  { "SA4784" : {"name": "Mark", "birthdate": None},
                "V410Z8" : { "name": "Vincent", "birthdate": "15/02/1989"},
                "CZ1094" : {"name": "Paul", "birthdate": "27/09/1994"}
              }
      

      那么你需要做的就是尝试直接访问它:

      try:
          print(data["V410Z8"]["name"])
      except KeyError:
          print("ID doesn't exist")
      >> "Vincent"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多