Home > Software engineering >  Why can i not recall an element from a list gotten from a separated string?
Why can i not recall an element from a list gotten from a separated string?

Time:08-16

    import requests
    import time
    response = requests.get('https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/London?unitGroup=metric&elements=temp,windspeed,sunrise,sunset,moonphase&key=(Not sharing my API key)&contentType=json')
    response = str(response)
    print(len(response))
    response = response.split(",")
    print(response[1])

What can i do to solve this? I know that it became a list, as now it has a lengh of 16, but why can i not call elements from that list?

CodePudding user response:

You should instead access the value of response on line 4 with:

response = response.json()

response will then be a dictionary representing a dictionary of the json returned from the request

CodePudding user response:

The variable response you get is a string with 16 characters in it; none of them is a comma (the string reads: "<Response [401]>"). That means that after your .split() with a comma as separator, you have a list with one element in it, so your indexation with [1] is out of bounds.

  • Related