String indices must be integers error – 字符串索引必须是整数错误

huangapple go评论58阅读模式
英文:

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 = {
	&quot;X-RapidAPI-Key&quot;: &quot;1b6ce2494dmshf74f9c461b4cdbbp1d3b11jsndd6ab0d8575c&quot;,
	&quot;X-RapidAPI-Host&quot;: &quot;api-football-v1.p.rapidapi.com&quot;
}

url_stats = &quot;https://api-football-v1.p.rapidapi.com/v3/teams/statistics&quot;

qstring_stats1 = {&quot;league&quot;:&quot;39&quot;,&quot;season&quot;:&quot;2022&quot;,&quot;team&quot;:&quot;33&quot;}

resp_stats = requests.request(&quot;GET&quot;, url_stats, headers=headers, params=qstring_stats1)
resp_stats = resp_stats.json()

print(type(resp_stats))
print(resp_stats)

for team in resp_stats[&quot;response&quot;]:
	print(str(team[&quot;team&quot;][&quot;id&quot;]) + &quot; - &quot; + (team[&quot;team&quot;][&quot;name&quot;]))
	print(resp_stats)

Output:

&lt;class &#39;dict&#39;&gt;

{&#39;get&#39;: &#39;teams/statistics&#39;, &#39;parameters&#39;: {&#39;league&#39;: &#39;39&#39;, &#39;season&#39;: &#39;2022&#39;, &#39;team&#39;: &#39;33&#39;}, &#39;errors&#39;: [], &#39;results&#39;: 11, &#39;paging&#39;: {&#39;current&#39;: 1, &#39;total&#39;: 1}, &#39;response&#39;: {&#39;league&#39;: {&#39;id&#39;: 39, &#39;name&#39;: &#39;Premier League&#39;, &#39;country&#39;: &#39;England&#39;, &#39;logo&#39;: &#39;https://media.api-sports.io/football/leagues/39.png&#39;, &#39;flag&#39;: &#39;https://media.api-sports.io/flags/gb.svg&#39;, &#39;season&#39;: 2022}, &#39;team&#39;: {&#39;id&#39;: 33, &#39;name&#39;: &#39;Manchester United&#39;}............

Traceback (most recent call last):
  File &quot;E:\Dropbox\CG\Coding\music_api\main.py&quot;, line 47, in &lt;module&gt;
    print(str(team[&quot;team&quot;][&quot;id&quot;]) + &quot; - &quot; + (team[&quot;team&quot;][&quot;name&quot;]))
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[&quot;response&quot;] dictionary, and they are strings, not dictionaries, thus you cannot get their "team" attribute (team[&quot;team&quot;])

According to the structure of resp_stats[&quot;response&quot;], there is info on one team only, and you can get it via:

team = resp_stats[&quot;response&quot;][&quot;team&quot;]
print(str(team[&quot;id&quot;]) + &quot; - &quot; + (team[&quot;name&quot;]))

&gt;&gt; 33 - Manchester United

If this is not the information you need, then you should analyze the structure of the resp_stats[&quot;response&quot;] dictionary and modify the code in accordance with it.

huangapple
  • 本文由 发表于 2023年2月27日 11:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定