错误numCraft =数据“工艺”
KeyError:“工艺”
我试图实现不同的计数和循环,但有困难,只是计数的数量不同的工艺。答案是两个,但不知道如何实现或理解JSON的概念
发布于 2022-10-28 00:36:32
下面是该url中json内容的(缩写)版本:
{
"message": "success",
"people": [
{
"name": "Cai Xuzhe",
"craft": "Tiangong"
},
{
"name": "Chen Dong",
"craft": "Tiangong"
},
{
"name": "Anna Kikina",
"craft": "ISS"
}
],
"number": 10
}正如您所看到的,顶级项目是message、people和number。没有名为craft的顶级项。
这就是为什么你会犯这个错误。
你可能想要这样的东西:
all_crafts = set()
for person in data['people']:
name = person['name']
craft = person['craft']
print(f'The astronaut {name} was in the craft {craft}')
all_crafts.add(craft)
print('There were this many different crafts:', len(all_crafts))https://stackoverflow.com/questions/74229489
复制相似问题