[Python] "list indices must be integers" error, when trying to use keys from a dictionary

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I tried to use the openweathermap.org rest API inside python. When I tried to assign a key from the dictionary I created with the JSON data this error occurred.

    -list indices must be integers or slices, not str


    I'm new to python and I couldn't find a solution to this matter. The code snip I wrote:

    import requests
    from pprint import pprint

    lokka = str(input("What is the location you need information of?"))
    #takes the location as "lokka"

    hellload = requests.get("http://api.openweathermap.org/data/2.5/weather?q="+ lokka +"&appid=xxxxxxxxxxxxxxxxx&units=metric")
    #the rest api's load will be taken to the account of hellload

    jputha = hellload.json()
    #json data will be converted to a dictionary
    #print (jputha)

    #---------------------------------------------------------
    #from now onward I'll be kickin the hell out the jsons
    long = str(jputha["coord"]["lon"])
    lat = str(jputha["coord"]["lat"])
    wthr = str(jputha["weather"]["main"])
    temp = str(jputha["main"]["temp"])
    winspd = str(jputha["wind"]["speed"])

    print(long)
    print(lat)
    print(wthr)
    print(temp)
    print(winspd)

    Continue reading...

Compartilhe esta Página