Пытается проанализировать JSON -данные, но не может установить a dict[index]
, поскольку переменная индекса присутствует только один раз в JSON, в то время как остальные ключи, значения требуют, чтобы al oop получил их , Я попробовал вложенное l oop, но безуспешно. Получение TypeError: list indices must be integers or slices, not str
. Любая обратная связь будет высоко оценена, полный пример ниже.
Большое спасибо,
class Premier_league:
def __init__(self, base_url='https://footballapi.pulselive.com/football'):
self.base_url = base_url
def get_standings(self, compSeasons):
url = self.base_url + '/compseasons/{}/standings'.format(compSeasons)
print(url)
# url -> https://footballapi.pulselive.com/football/compseasons/274/standings
params = (
('pageSize', '100'),
)
response = requests.get(url, params = params).json() # request to obtain the team info
all_standings = response["tables"][0]['entries']
info_standings = response['compSeason']
standings = {} #Store all standings
#loop to get all info for all standings
for info in info_standings:
standing_id = info['label']
index = standing_id
standings[index] = \
{'id' : info['id']}
for standing in all_standings:
standings[index] = \
{
'team' : standing['team']['name'],
'team_id' : standing['team']['club']['id'],
'position' : standing['position'],
'overall' : standing['overall'],
'home' : standing['home'],
'away' : standing['away'],
}
f = open("standings_" + str(compSeasons) + ".json","w")
# pretty prints and writes the same to the json file
f.write(json.dumps(standings,indent=4, sort_keys=False))
f.close()
if __name__ == "__main__":
prem = Premier_league()
prem.get_standings(274)
Ожидаемый результат:
{
"2019/20": {
'id' : info['id']
'team' : standing['team']['name'],
'team_id' : standing['team']['club']['id'],
'position' : standing['position'],
'overall' : standing['overall'],
'home' : standing['home'],
'away' : standing['away'],
},
"2019/20": {
'id' : info['id']
'team' : standing['team']['name'],
'team_id' : standing['team']['club']['id'],
'position' : standing['position'],
'overall' : standing['overall'],
'home' : standing['home'],
'away' : standing['away'],
},
}