如何从字典中切片天气数据并重写?



我从weatherbit-api那里得到了这个数据。我真的需要帮助来切割数据。

下面的是我的代码:

from weatherbit.api import Api
api_key = "******************"
api = Api(api_key)
Location = "Tokyo"
Day_of_Forecast = '3'
forecast = api.get_forecast(days = Day_of_Forecast, city= Location)
forecast_list = forecast.get_series(['temp', 'max_temp', 'min_temp', 'weather'])
print(forecast_list)

它返回如下:

[{'temp': 33, 'max_temp': 36.5, 'min_temp': 29.5, 'weather': {'icon': 'c03d', 'code': 803, 'description': 'Broken clouds'}, 'datetime': datetime.datetime(2019, 8, 7, 0, 0)},
{'temp': 31.3, 'max_temp': 35.3, 'min_temp': 28.8, 'weather': {'icon': 'c04d', 'code': 804, 'description': 'Overcast clouds'}, 'datetime': datetime.datetime(2019, 8, 8, 0, 0)},
{'temp': 30.7, 'max_temp': 34.4, 'min_temp': 28.4, 'weather': {'icon': 'c03d', 'code': 803, 'description': 'Broken clouds'}, 'datetime': datetime.datetime(2019, 8, 9, 0, 0)}]

因此,使用上面的列表,我如何对其进行切片并重写它以返回如下结果:

The weather forecast for Tokyo is:
On 7th of August 2019, the weather forecast is Broken clouds. The temperature will be 33 celsius degree, the maximum temperature will be 36.5 celsius degree and minimum temperature will be 29.5 degree.
On 8th of August , the weather forecast is Overcast clouds. The temperature will be 31.3 celsius degree, the maximum temperature will be 35.3 celsius degree and minimum temperature will be 28.8 degree.
On 9th of August , the weather forecast is Broken clouds. The temperature will be 30.7 celsius degree, the maximum temperature will be 34.4 celsius degree and minimum temperature will be 28.4 degree.

很抱歉这个菜鸟问题,但这是我实践的一部分,我已经为此苦苦挣扎了好几天。 非常感谢您的帮助。非常感谢。 随意复制列表或任何内容。

def better_forecast_list(forecast_list):
better_list = []
for forecast in forecast_list:
better_list.append(
f'''Today is {forecast['datetime']: %d %B %Y} 
weather is {forecast['weather']['description']} 
temp will be {forecast['temp']} 
''')
return better_list

顺便说一下,我用了f字符串。它仅在python 3.7及以上可用

最新更新