尝试访问该键确实存在的字典时出现键错误

问题描述 投票:0回答:1

因此,我正在构建这个应用程序,当我尝试访问球队名称时,我可以使用此 api 查看实时板球比分,这给了我一个关键错误,该错误没有意义,因为该密钥确实存在。

import requests

api_key = ""  



url_currentmatches = f"https://api.cricapi.com/v1/cricScore?apikey={api_key}"
response = requests.get(url_currentmatches)
result = response.json()




match_number=-1
amount_of_matches = len(result["data"])
while True:
    match_number += 1
    if match_number == amount_of_matches:
        break
    else: 
        name1 = result["data"][match_number]["teamInfo"][0]["name"]
        name2 = result["data"][match_number]["teamInfo"][1]["name"]
        

        important_countries = ["Pakistan","New Zealand","Australia","Sri Lanka","South Africa","West Indies","England","India"]
        for country in important_countries:
            
            if  name1.find(country) != -1 or name2.find(country) != -1:
                print("name matche: " + name1,name2)
                print("We have the result:", name1," vs ",name2)
                print(country)

       

            else:
                pass

这是其中一场比赛的数据片段:

{
    "apikey": "",
    "data": [
        {
            "id": "50f2cc52-c119-4323-b320-d004b8262761",
            "name": "Northern Cape vs Mpumalanga Rhinos, 11th Match",
            "matchType": "test",
            "status": "Northern Cape won by an innings and 96 runs",
            "venue": "Diamond Oval, Kimberley",
            "date": "2023-12-20",
            "dateTimeGMT": "2023-12-20T08:00:00",
            "teams": [
                "Northern Cape",
                "Mpumalanga Rhinos"
            ],
            "teamInfo": [
                {
                    "name": "Mpumalanga Rhinos",
                    "shortname": "MPR",
                    "img": "https://g.cricapi.com/iapi/49-637991993271802283.webp?w=48"
                },
                {
                    "name": "Northern Cape",
                    "shortname": "NCAPE",
                    "img": "https://g.cricapi.com/iapi/61-637991981464151764.webp?w=48"
                }
            ],
            "score": [
                {
                    "r": 185,
                    "w": 10,
                    "o": 42.1,
                    "inning": "Mpumalanga Rhinos Inning 1"
                },
                {
                    "r": 502,
                    "w": 10,
                    "o": 153.4,
                    "inning": "Northern Cape Inning 1"
                },
                {
                    "r": 221,
                    "w": 10,
                    "o": 62,
                    "inning": "Mpumalanga Rhinos Inning 2"
                }
            ],
            "series_id": "e3bd2125-82e2-4841-a097-c681d46c7a60",
            "fantasyEnabled": true,
            "bbbEnabled": false,
            "hasSquad": true,
            "matchStarted": false,
            "matchEnded": true
        }]} 

数据可能有一些未封闭的列表或字典,因为我删除了很多匹配项并将其单挑为一个,所以请忽略这一点,但所有其他匹配项都具有相同的格式

这是我收到的错误:

Exception has occurred: KeyError
'teamInfo'
  File "D:\Window Widgets\Dump.py", line 42, in <module>
    name1 = result["data"][match_number]["teamInfo"][0]["name"]
KeyError: 'teamInfo'
python api dictionary keyerror
1个回答
0
投票

首先尝试 print(result["data"][match_number]) 并检查是否有这样的键, 其次请记住,由于您在循环开始时递增“match_number”,因此您永远不会触及结果['data'][0]

© www.soinside.com 2019 - 2024. All rights reserved.