Почему я получаю сообщение «TypeError: строковые индексы должны быть целыми числами»?Python

Программы на Python
Anonymous
Почему я получаю сообщение «TypeError: строковые индексы должны быть целыми числами»?

Сообщение Anonymous »

Код: Выделить всё

import nextcord
from nextcord.ext import commands
from config import TOKEN, API_KEY
import aiohttp

bot = commands.Bot(command_prefix = "!", intents = nextcord.Intents.all())

@bot.event
async def on_ready():
print("Bot has connected to Discord")

@bot.command()
async def weather(ctx: commands.Context, *, city):
url = "http://api.weatherapi.com/v1/current.json"
params = {
"key": API_KEY,
"q": city
}

async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as responses:
data = await responses.json()

print(data)
location = data["location"]["name"]["localtime"]
Я постоянно получаю ошибку TypeError в этой строке:

Код: Выделить всё

location = data["location"]["name"]["localtime"]
>Ошибка типа:

Код: Выделить всё

string indices must be integers, not 'str'
В API погоды информация хранится во вложенных словарях. Я получаю доступ к этому значению, используя location = data["outer_key"]["inner_key"]
Я передаю команду !weather "city_name"
Что я делаю не так?

Подробнее здесь: https://stackoverflow.com/questions/784 ... e-integers

Вернуться в «Python»