Click here to Skip to main content
15,397,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get "rain" data in 48 hours in the openweathermap website using API. It was a list in dictionary like this:
'rain': {'1h': 0.47}

The below was my code when I didn't print "rain" data

<pre>
import pprint as pp
import requests
import json
import time
from datetime import datetime

url = "https://api.openweathermap.org/data/2.5/onecall?"
loc = "lat=34.98715&lon=135.73175"
excl = "&exclude=current,minutely,daily"
unit = "&units=metric"
lang = "&lang=ja"
key = "730fc18044ede05533d052b1be72ce93"

r = requests.get( url+loc+excl+unit+lang+"&appid="+key)
rj = r.json()

for i in range(len(rj["hourly"])):
    data = rj["hourly"][i]
    if i != None:
        time_stamp = data["dt"]
        date_time = datetime.fromtimestamp(time_stamp)
        print(date_time)
        pp.pprint(f"clouds : {data['clouds']} %")
        pp.pprint(f"dt : {time_stamp} UTC")
        pp.pprint(f"feels_like : {data['feels_like']}")
        pp.pprint(f"humidity : {data['humidity']} %")
        pp.pprint(f"pop : {data['pop']}")
        pp.pprint(f"pressure : {data['pressure']} hPa")
        pp.pprint(f"temp : {data['temp']}")
        pp.pprint(f"wind_speed : {data['wind_speed']}")
        print()


When I tried to get all the weather info in Json format it was like this:

'wind_deg': 99,
     'wind_gust': 2.55,
     'wind_speed': 3.27},
    {'clouds': 17,
     'dew_point': 15.66,
     'dt': 1654758000,
     'feels_like': 25.26,
     'humidity': 58,
     'pop': 0.48,
     'pressure': 1012,
     'rain': {'1h': 0.24},
     'temp': 25.17,
     'uvi': 1.49,
     'visibility': 10000,
     'weather': [{'description': '小雨',
                  'icon': '10d',
                  'id': 500,
                  'main': 'Rain'}],


As you can see the 'rain' data is in a list and I want to take the specific datas include that one among those in my above code

What I have tried:

When I used data['rain'] or data['rain']['1h'] in the pp.pprint commands like this:
pp.pprint(f"rain : {data['rain']}") 

it showed error: Keys Errors: rain
Posted
Updated 7-Jun-22 22:05pm
v3

1 solution

I just ran your code and printed the first data entry, which shows:
{'dt': 1654671600, 'temp': 23.04, 'feels_like': 23.04, 'pressure': 1009, 'humidity': 63, 'dew_point': 15.62, 'uvi': 1.46, 'clouds': 93, 'visibility': 10000, 'wind_speed': 1.72, 'wind_deg': 6, 'wind_gust': 2.96, 'weather': [{'id': 
804, 'main': 'Clouds', 'description': '厚い雲', 'icon': '04d'}], 'pop': 0.08}

As you can see there is no item titled "rain".
   
Comments
Member 14629414 8-Jun-22 3:47am
   
@Richard MacCutchan Add the command : pp.pprint(f"rain : {data['rain']}") to the print sections of the codes and you will see the errors
Richard MacCutchan 8-Jun-22 4:06am
   
Yes, of course you will, since the data map does not contain an item labelled "rain". You can only refer to map entries that exist, i.e.
'dt'
'temp'
'feels_like'
'pressure'
'humidity'
'dew_point'
'uvi'
'clouds'
'visibility'
'wind_speed'
'wind_deg'
'wind_gust'
'weather': containing: {
    'id'
    'main'
    'Clouds'
    'description'
    'icon'
}
and 'pop'
Member 14629414 8-Jun-22 4:09am
   
When I tried to access all the data (example for 'current') I got this:
'wind_deg': 99,
'wind_gust': 2.55,
'wind_speed': 3.27},
{'clouds': 17,
'dew_point': 15.66,
'dt': 1654758000,
'feels_like': 25.26,
'humidity': 58,
'pop': 0.48,
'pressure': 1012,
'rain': {'1h': 0.24},
'temp': 25.17,
'uvi': 1.49,
'visibility': 10000,
'weather': [{'description': '小雨',
'icon': '10d',
'id': 500,
'main': 'Rain'}],

you can see the list 'rain' which is the amount rain for 1h
Richard MacCutchan 8-Jun-22 4:16am
   
Yes, and I guess that the people who designed the system decided that if there was no rain then they did not need to add it to the data. Probably the same for hail, snow etc. Since the data is not fixed (that's why they use JSON) you need to find the items that actually have values. See 5. Data Structures — Python 3.10.5 documentation[^], for more on how to access the contents of a Dictionary.
Member 14629414 8-Jun-22 4:19am
   
thanks for that

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900