我希望有人能给我指出正确的方向。我对Python还比较陌生。我正在使用一个openergastapi,尝试导入/解析一个json文件,并对一些元素进行过滤。我可以毫无问题地打印整个json文件,但是只要我尝试在driverId上进行过滤,它就会失败。我做错什么了?提前感谢:
import json
import requests
response = requests.get("https://ergast.com/api/f1/current/last/results.json?")
data = response.json()
print (data["driverId"])
编辑:我得到的答复是:
Traceback (most recent call last):
File "python", line 6, in <module>
KeyError: 'driverId'
键
driverId
嵌套在数组中,数组中的Results
依次在Races
内,数组中的RaceTable
依次在MRData
内。因此,要获得第一个driveId,您必须使用以下代码如果您想要得到
driverId
的所有值,那么您将编写一个for循环相关问题 更多 >
编程相关推荐