Reputation: 23
All the variables that store the API data:
formatted_data = json_data['list'][0]['main'] # Extracting the main description from the API
formatted_data3 = json_data['list'][0]['weather']['description'] # Extracting the weather description from the API
formatted_data2 = json_data['list'][0]['main']['temp'] # Extracting the temperature description from the API
formatted_data4 = json_data['list'][0]['main']['temp_min']
formatted_data5 = json_data['list'][0]['main']['temp_max']
formatted_data6 = json_data['list'][0]['clouds']['all']
Can anyone help me out with this error?
Traceback (most recent call last):
File "MainWeatherAPI.py", line 21, in <module>
print(ForecastAPIFunction.getForecast(city, country))
File "/home/codio/workspace/Weatherbot/ForecastAPIFunction.py", line
13, in getForecast
formatted_data3 = json_data['list'][0]['weather']['description']
#Extracting the weather description from the API
EDIT:
Code that is relevant to what I have tried so far:
while True:
address = input("Address: ")
if address == 'quit' or address == 'q':
break
url = main_api + urllib.parse.urlencode({'address': address})
json_data = requests.get(url).json()
#print(url)
json_status = json_data['status'] print('API Status: ' + json_status + '\n')
if json_status == 'OK':
for each in json_data['results'][0]['address_components']:
print(each['long_name'])
EDIT:
Sample API data that correlates to all the variables that I have used to store the data. This is also relevant to the error that I am receiving.
{
'cnt': 40,
'city': {
'name': 'Coventry',
'country': 'GB',
'id': 2652221,
'coord': {
'lat': 52.4066,
'lon': -1.5122}
},
'message': 0.165,
'list': [
{'sys': {'pod': 'n'},
'dt_txt': '2017-11-23 21:00:00',
'weather': [
{
'main': 'Clouds',
'icon': '04n',
'id': 803,
'description': 'broken clouds'
}
],
'rain': {},
'main': {
'temp_max': 278.37,
'grnd_level': 1005.61,
'temp': 278.37,
'sea_level': 1018.48,
'humidity': 79,
'temp_kf': 0.15,
'pressure': 1005.61,
'temp_min': 278.226},
'clouds': {'all': 56},
'dt': 1511470800,
'wind': {'deg':''}
}
Upvotes: 0
Views: 518
Reputation: 2018
try this
formatted_data3 = json_data['list'][0]['weather'][0]['description']
the weather element is array as well
Upvotes: 1