AndyReifman
AndyReifman

Reputation: 1844

Deciphering a JSON dictionary and getting keys

I'm not sure if my title does a very good job of describing what I'm looking for.

I have a program that is accessing This JSON data and storing it in a dictionary called forecast

Currently I have it printing out the weather information in this for-loop

day_num = 1
for day in forecast['list']:
    print("Day: ",day_num)
    print(day)
    print(day['weather'][0]['description']) #clear, cloudy, etc
    print("Cloud Cover: ", day['clouds'])
    print("Temp: ",round(day['temp']['day']-273.15,1),"degrees C")
    print("Temp Min: ", round(day['temp']['min']-273.15, 1), "degrees C")
    print("Temp Max: ", round(day['temp']['max']-273.15, 1),"degrees C")
    print("Humidity: ", day['humidity'],"%")
    print("Wind Speed:", day['speed'], "m/s")
    print()
    day_num = day_num + 1

This does a great job of printing out all the information I want for all days listed on the website. My issue is that I'm having trouble identifying the key/information I need for if I want it to print out just one day. (Basically, what day is grabbing in the for loop)

Upvotes: 0

Views: 33

Answers (2)

John La Rooy
John La Rooy

Reputation: 304463

For the first day:

day = forecast['list'][0]

For the second day:

day = forecast['list'][1]

etc.

Upvotes: 2

AtAFork
AtAFork

Reputation: 376

If you want the first day in the list you can just add a break to the end of the for loop.

Upvotes: 0

Related Questions