使用 Python 在 JSON 中查找值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40827356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find a value in JSON using Python
提问by antonioag
I've previously succeeded in parsing data from a JSON file, but now I'm facing a problem with the function I want to achieve. I have a list of names, identification numbers and birthdate in a JSON. What I want to get in Python is to be able to let a user input a name and retrieve his identification number and the birthdate (if present).
我之前已经成功解析了 JSON 文件中的数据,但现在我面临着我想要实现的功能的问题。我有一个 JSON 格式的姓名、身号码和生日列表。我想在 Python 中得到的是能够让用户输入一个名字并检索他的身号码和生日(如果有的话)。
This is my JSON example file:
这是我的 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"
}
]
To be clear, I want to input "V410Z8" and get his name and his birthdate.
明确地说,我想输入“V410Z8”并获取他的姓名和生日。
I tried to write some code in Python but I only succeed in searching for “id_number” and not for what is inside “id_number” for example "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
Thank you for your support, guys :)
谢谢小伙伴们的支持:)
回答by DeepSpace
You have to iterate over the list of dictionaries and search for the one with the given id_number
. Once you find it you can print the rest of its data and break, assuming id_number
is unique.
您必须遍历字典列表并搜索具有给定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
If you have control over the data structure, a more efficient way would be to use the id_number
as a key (again, assuming id_number
is unique):
如果您可以控制数据结构,更有效的方法是将id_number
用作键(同样,假设id_number
是唯一的):
data = { "SA4784" : {"name": "Mark", "birthdate": None},
"V410Z8" : { "name": "Vincent", "birthdate": "15/02/1989"},
"CZ1094" : {"name": "Paul", "birthdate": "27/09/1994"}
}
Then all you need to do is try to access it directly:
然后你需要做的就是尝试直接访问它:
try:
print(data["V410Z8"]["name"])
except KeyError:
print("ID doesn't exist")
>> "Vincent"
回答by Devandran
Using lamdain Python
在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"
}
]
Using Lambda and filter
使用 Lambda 和过滤器
print(list(filter(lambda x:x["id_number"]=="CZ1094",data)))
Output
输出
[{'id_number': 'CZ1094', 'name': 'Paul', 'birthdate': '27/09/1994'}]
回答by Sanket Sawardekar
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)
Output should be
输出应该是
[{
"id_number": "CZ1094",
"name": "Paul",
"birthdate": "26/09/1994"
}]