Python 包含子键和子值的JSON文件
我有一个json文件,如下所示:Python 包含子键和子值的JSON文件,python,json,dictionary,Python,Json,Dictionary,我有一个json文件,如下所示: {"people": [{"name": "carlota", "birthday": "11/12/1945"}, {"name": "mariana", "birthday": "25/08/1930"}, {"name": "agustina", "birthday": "17/11/1943"}]} 如何在不必创建新词典的情况下获取“people”中的信息?因为这是我唯一能做的。这是我的密码: imp
{"people": [{"name": "carlota", "birthday": "11/12/1945"},
{"name": "mariana", "birthday": "25/08/1930"},
{"name": "agustina", "birthday": "17/11/1943"}]}
如何在不必创建新词典的情况下获取“people”中的信息?因为这是我唯一能做的。这是我的密码:
import json
with open("cumple.txt", "r") as f: #cumple.txt is the file where my JSON is
info = json.load(f)
l=[]
s=[]
print('we know the birthday of:')
for i in info['people']:
print(i['name'])
print(i['birthday'])
l.append(i['name'])
s.append(i['birthday'])
c=input("who's birthday want to know ? ")
dictionary = dict(zip(l, s))
if c in l:
print('{} cumple el'.format(c), dictionary[c] )
else:
print('we don\'t know the birthday of {}'.format(c)
字典通常是最好的方法。没有字典,你必须循环
for i in info:
if i['name'] == c:
print('{} cumple el {}'.format(c, i['birthday']))
break
else:
print('we don\'t know the birthday of {}'.format(c)
else:
for循环的块在循环正常结束时执行,而不是从break
如果您知道如何访问字典值,则所需的输出是为您准备的,如果字典中的c
,则应使用而不是如果l中的c
则您的代码未编译。您缺少结尾处的)
请更具体地说明问题所在。