#current forecast
current_api = 'api.openweathermap.org/data/2.5/weather?zip='
current_url_zip = current_api + urllib.parse.urlencode({'Zip': zip})
#current_url_key =
json_data = requests.get(future_url_key).json()
#print (json_data)
future_temp_day_0 = json_data['list'][0]['main']['temp'] #current day
future_temp_day_1 = json_data['list'][1]['main']['temp'] #tomorrow
future_description_day_0 = json_data['list'][0]['weather']['description'] #current description
future_description_day_1 = json_data['list'][1]['weather']['description'] #current description
#Kelvin to F conversion
fTemp_0 = int((future_temp_day_0 - 273.15) * (9/5) + (32))
fTemp_1 = int((future_temp_day_1 - 273.15) * (9/5) + (32))
So I am using the openweathermap api. I want to be able to pull the current day [temperature][weather description] and tomorrows [temperature][weather description]. The problem is when I try to reference the [weather description] it pulls it from json_data['list'][3]
and not json_data['list'][1]
. It iterates to the next spot even though I am referencing the [1]
item.
所以我使用的是openweathermap api。我希望能够拉当前[温度] [天气描述]和明天[温度] [天气描述]。问题是,当我尝试引用[天气描述]时,它从json_data ['list'] [3]而不是json_data ['list'] [1]中提取它。即使我引用[1]项,它也会迭代到下一个位置。
{
"cod":"200",
"message":0.0122,
"cnt":40,
"list":[
{
"dt":1519074000,
"main":{
"temp":283.99,
"temp_min":281.801,
"temp_max":283.99,
"pressure":989.94,
"sea_level":1029.29,
"grnd_level":989.94,
"humidity":52,
"temp_kf":2.19
},
"weather":[
{
"id":801,
"main":"Clouds",
"description":"few clouds",
"icon":"02d"
}
],
"clouds":{
"all":20
},
"wind":{
"speed":3.36,
"deg":325.001
},
"rain":{
},
"sys":{
"pod":"d"
},
"dt_txt":"2018-02-19 21:00:00"
},
{
"dt":1519084800,
"main":{
"temp":282.64,
"temp_min":281.177,
"temp_max":282.64,
"pressure":990.6,
"sea_level":1029.94,
"grnd_level":990.6,
"humidity":47,
"temp_kf":1.46
},
"weather":[
{
"id":802,
"main":"Clouds",
"description":"scattered clouds",
"icon":"03n"
}
],
"clouds":{
"all":36
},
"wind":{
"speed":3.17,
"deg":319.502
},
"rain":{
},
"sys":{
"pod":"n"
},
"dt_txt":"2018-02-20 00:00:00"
}
2 个解决方案
#1
0
The weather
key contains a list of dicts, so you should use [0]
if you want the description of the first entry of the list:
天气键包含一个dicts列表,因此如果您想要列表中第一个条目的描述,则应使用[0]:
future_description_day_0 = json_data['list'][0]['weather'][0]['description']
future_description_day_1 = json_data['list'][1]['weather'][0]['description']
#2
0
future_description_day_1 = json_data['list'][0]['weather']
data = [P['description'] for P in future_description_day_1 if 'description' in P]
try:
if(len(data) == 4): # You can set the length of the data if it is more than 4 which i had been described.
weather_desc_1 = data[0]
weather_desc_2 = data[1]
weather_desc_3 = data[2]
weather_desc_4 = data[3]
print 'Weather desc_1: ',weather_desc_1
print 'Weather desc_2: ',weather_desc_2
print 'Weather desc_3: ',weather_desc_3
print 'Weather desc_4: ',weather_desc_4
else:
weather_desc_1 = 'Null'
weather_desc_2 = 'Null'
weather_desc_3 = 'Null'
weather_desc_4 = 'Null'
except IndexError:
print 'No wether description available from the data '
#1
0
The weather
key contains a list of dicts, so you should use [0]
if you want the description of the first entry of the list:
天气键包含一个dicts列表,因此如果您想要列表中第一个条目的描述,则应使用[0]:
future_description_day_0 = json_data['list'][0]['weather'][0]['description']
future_description_day_1 = json_data['list'][1]['weather'][0]['description']
#2
0
future_description_day_1 = json_data['list'][0]['weather']
data = [P['description'] for P in future_description_day_1 if 'description' in P]
try:
if(len(data) == 4): # You can set the length of the data if it is more than 4 which i had been described.
weather_desc_1 = data[0]
weather_desc_2 = data[1]
weather_desc_3 = data[2]
weather_desc_4 = data[3]
print 'Weather desc_1: ',weather_desc_1
print 'Weather desc_2: ',weather_desc_2
print 'Weather desc_3: ',weather_desc_3
print 'Weather desc_4: ',weather_desc_4
else:
weather_desc_1 = 'Null'
weather_desc_2 = 'Null'
weather_desc_3 = 'Null'
weather_desc_4 = 'Null'
except IndexError:
print 'No wether description available from the data '