英文:
Access data from a dictionary within a list within a list from api
问题
{
"get": "standings",
"parameters": {
"league": "12",
"season": "2019-2020"
},
"errors": [],
"results": 1,
"response": [
[
{
"position": 1,
"stage": "NBA - Regular Season",
"group": {
"name": "Western Conference",
"points": null
},
...
}
]
]
}
英文:
{
"get": "standings",
"parameters": {
"league": "12",
"season": "2019-2020"
},
"errors": [],
"results": 1,
"response": [
[
{
"position": 1,
"stage": "NBA - Regular Season",
"group": {
"name": "Western Conference",
"points": null
},
I am being returned information from an API that I would like to access in Python. I would like to access the "position" element. I have tried the following code to no avail:
"position": response['response'][0]['id'],
This seems to give me either a key Error or an Attribute Error. Can anyone give me some help with this?
答案1
得分: 1
你应该这样做:
position = response["response"][0][0]["position"]
print(position) # 1
英文:
You should do this instead:
position = response["response"][0][0]["position"]
print(position) # 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论