我有一个json文件“contacts.json”,其中包含:
{
"Lisa": {
"birthday": "09-02-1990"
},
"Marge": {
"birthday": "05-01-2010"
},
"Bart": {
"birthday": "23-09-1935"
},
"Homer": {
"birthday": "22-11-1990"
}
}
现在我需要编写一个代码来提取用户输入的输入名称的生日。 我写了这段代码,但它不起作用
import json
name = input('Enter name of the person you want to see his/her birthday ')
with open('contacts.json') as file:
data = json.load(file)
for i in data[i]:
if name == data[i]:
print(data[i]['Birthday'])
else:
print('Person not found')
file.close()
此代码可能对您有所帮助
注意:对于Python2.x,使用arr.iteritems()而不是arr.items()
这应该满足您的要求:
相关问题 更多 >
编程相关推荐