The goal of this script is to show a six day forecast from the command line. It would include the date, temperature, and one word summary. In order to pull up the necessary information, I used an API from https://openweathermap.org/api
Formatting
The output of the program should be formatted as the following:
**Sample Output:** date, average temp, summary
Nov 30 – 23 C, sunny
Des 1 – 30 C, cloudy
Des 2 – 30 C, cloudy
Code
import datetime, requests, json, pprint
res = MY_API
To begin, I import all the necessary libraries and make a request for the weather information with my API key.
for i in range(6):
daily = weather['daily']
date = daily[i]['dt']
date_time = datetime.datetime.fromtimestamp( date )
best = date_time.strftime( "%h %d")
temp = int(daily[i]['temp']['day'])
description = daily[i]['weather'][0]['main']
print(best, "-", temp, "°C,", description)
Then, I create a loop for printing the weather info for each of the six days. Using the dictionary structure to help me, I then extracted all the needed info from the json text after converting it to a list of dictionaries. The date of the weather information in the API is written in Unix time, so I had to use the datetime library to convert it to month and day. Lastly, I compile all the extracted information to print the weather status.