英文:
Reading API data - String indices must be integers error
问题
我正在尝试处理API数据,但是使用我已有的代码来访问字典时,我遇到了"String indices must be integers"错误。有没有关于如何让字符串查找字典中的键的想法(我希望不使用整数,如果我使用整数,就会出现超出范围的错误)。
import requests
import json
headers = {
"X-RapidAPI-Key": "1b6ce2494dmshf74f9c461b4cdbbp1d3b11jsndd6ab0d8575c",
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com"
}
url_stats = "https://api-football-v1.p.rapidapi.com/v3/teams/statistics"
qstring_stats1 = {"league": "39", "season": "2022", "team": "33"}
resp_stats = requests.request("GET", url_stats, headers=headers, params=qstring_stats1)
resp_stats = resp_stats.json()
print(type(resp_stats))
print(resp_stats)
for team in resp_stats["response"]:
print(str(team["team"]["id"]) + " - " + (team["team"]["name"]))
print(resp_stats)
输出:
<class 'dict'>
{'get': 'teams/statistics', 'parameters': {'league': '39', 'season': '2022', 'team': '33'}, 'errors': [], 'results': 11, 'paging': {'current': 1, 'total': 1}, 'response': {'league': {'id': 39, 'name': 'Premier League', 'country': 'England', 'logo': 'https://media.api-sports.io/football/leagues/39.png', 'flag': 'https://media.api-sports.io/flags/gb.svg', 'season': 2022}, 'team': {'id': 33, 'name': 'Manchester United'}............
Traceback (most recent call last):
File "E:\Dropbox\CG\Coding\music_api\main.py", line 47, in <module>
print(str(team["team"]["id"]) + " - " + (team["team"]["name"]))
TypeError: string indices must be integers
英文:
I am trying to process api data, but using the code I have to access a dict, I get the "String indices must be integers" error. Any ideas on how to get strings to find the keys in the dict (would prefer not to use integers, and if i do I get the out of range error).
import requests
import json
headers = {
"X-RapidAPI-Key": "1b6ce2494dmshf74f9c461b4cdbbp1d3b11jsndd6ab0d8575c",
"X-RapidAPI-Host": "api-football-v1.p.rapidapi.com"
}
url_stats = "https://api-football-v1.p.rapidapi.com/v3/teams/statistics"
qstring_stats1 = {"league":"39","season":"2022","team":"33"}
resp_stats = requests.request("GET", url_stats, headers=headers, params=qstring_stats1)
resp_stats = resp_stats.json()
print(type(resp_stats))
print(resp_stats)
for team in resp_stats["response"]:
print(str(team["team"]["id"]) + " - " + (team["team"]["name"]))
print(resp_stats)
Output:
<class 'dict'>
{'get': 'teams/statistics', 'parameters': {'league': '39', 'season': '2022', 'team': '33'}, 'errors': [], 'results': 11, 'paging': {'current': 1, 'total': 1}, 'response': {'league': {'id': 39, 'name': 'Premier League', 'country': 'England', 'logo': 'https://media.api-sports.io/football/leagues/39.png', 'flag': 'https://media.api-sports.io/flags/gb.svg', 'season': 2022}, 'team': {'id': 33, 'name': 'Manchester United'}............
Traceback (most recent call last):
File "E:\Dropbox\CG\Coding\music_api\main.py", line 47, in <module>
print(str(team["team"]["id"]) + " - " + (team["team"]["name"]))
TypeError: string indices must be integers
答案1
得分: 0
There is something wrong with the last for
cycle: you loop through keys of the resp_stats["response"]
dictionary, and they are strings, not dictionaries, thus you cannot get their "team" attribute (team["team"]
).
According to the structure of resp_stats["response"]
, there is info on one team only, and you can get it via:
team = resp_stats["response"]["team"]
print(str(team["id"]) + " - " + (team["name"]))
33 - Manchester United
If this is not the information you need, then you should analyze the structure of the resp_stats["response"]
dictionary and modify the code in accordance with it.
英文:
There is something wrong with the last for
cycle: you loop through keys of the resp_stats["response"]
dictionary, and they are strings, not dictionaries, thus you cannot get their "team" attribute (team["team"]
)
According to the structure of resp_stats["response"]
, there is info on one team only, and you can get it via:
team = resp_stats["response"]["team"]
print(str(team["id"]) + " - " + (team["name"]))
>> 33 - Manchester United
If this is not the information you need, then you should analyze the structure of the resp_stats["response"]
dictionary and modify the code in accordance with it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论