【发布时间】: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
谢谢你们的支持,伙计们:)
【问题讨论】: