Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:KeyError,但键和值都存在_Python_Json_Python 3.x_Api - Fatal编程技术网

Python:KeyError,但键和值都存在

Python:KeyError,但键和值都存在,python,json,python-3.x,api,Python,Json,Python 3.x,Api,我正在从OpenDotaAPI获取信息。我之前已经取出了一个包含大约160个匹配ID的.csv列表,我希望从中获得更多信息,然后将某些值附加到列表中 使用下面的代码,在列表中循环时,我收到一个KeyError for x in finallist: matchinfo = requests.get("https://api.opendota.com/api/matches/{}".format(x)).json()["match_id"] print(matchinfo) Ke

我正在从OpenDotaAPI获取信息。我之前已经取出了一个包含大约160个匹配ID的.csv列表,我希望从中获得更多信息,然后将某些值附加到列表中

使用下面的代码,在列表中循环时,我收到一个KeyError

for x in finallist:
    matchinfo = requests.get("https://api.opendota.com/api/matches/{}".format(x)).json()["match_id"]
    print(matchinfo)
KeyError是在某个ID上引发的,但是如果我使用下面的代码,我不会得到KeyError,我会看到信息

matchspec = requests.get("https://api.opendota.com/api/matches/4184421518").json()['match_id']
matchspec

因此,通过这个,4184421518打印信息,但在循环中,它在该ID处停止,在该ID下面还有几个ID。为什么我在for循环中收到一个keyrerror,但在我专门询问信息时却没有收到

Opendota api的速率限制为60个请求/分钟。请确保未超过该速率,因为这似乎是api响应的错误。

Opendota api的速率限制为60个请求/分钟。请确保未超过该速率,因为它似乎是api响应的错误。

我认为需要在{}中添加位置参数。例如,它应该是:matchinfo=requests.get(“{0}”).format(str(x)).json()[“match_id”]Opendota api的速率限制为60个请求/分钟。请确保您没有超过该速率,因为api响应似乎有错误。您的解决方案不起作用,但我添加了{0}在我的代码中,它循环了更多的ID,但最终在完成列表之前返回了一个KeyError。@ShivamSingh这似乎是引发KeyError的原因!我认为您需要在{}中添加位置参数。例如,它应该是:matchinfo=requests.get(“{0}”).format(str(x)).json()[“match_id”]Opendota api的速率限制为60个请求/分钟。请确保您没有超过该速率,因为api响应似乎有错误。您的解决方案不起作用,但我添加了{0}在我的代码中,它循环了更多的ID,但最终在完成列表之前返回了一个KeyError。@ShivamSingh这似乎是引发KeyError的原因!