-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject 1
45 lines (35 loc) · 1.99 KB
/
project 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import requests
#import os
from datetime import datetime
api_key = '3ac60f6dd9731144edb2b209880148b9'
location = input("Enter the city name: ")
complete_api_link = "https://api.openweathermap.org/data/2.5/weather?q="+location+"&appid="+api_key
api_link = requests.get(complete_api_link)
api_data = api_link.json()
#create variables to store and display data
temp_city = ((api_data['main']['temp']) - 273.15)
weather_desc = api_data['weather'][0]['description']
hmdt = api_data['main']['humidity']
wind_spd = api_data['wind']['speed']
date_time = datetime.now().strftime("%d %b %Y | %I:%M:%S %p")
print ("-------------------------------------------------------------")
print ("Weather Stats for - {} || {}".format(location.upper(), date_time))
print ("-------------------------------------------------------------")
print ("Current temperature is: {:.2f} deg C".format(temp_city))
print ("Current weather desc :",weather_desc)
print ("Current Humidity :",hmdt, '%')
print ("Current wind speed :",wind_spd ,'kmph')
print("====================================================")
# making a list so that i can print the info to a txt
txtlist = [temp_city,weather_desc,hmdt,wind_spd,date_time]
#using open() buit-in function to write to a text file
with open("textfile.txt" , mode= 'w' ,encoding= 'utf-8') as f :
#encoding = utf-8 for linux and cp1252 for win
f.write("------------------------------------------------------------- \n ")
f.write("Weather Stats for - {} || {}".format(location.upper(), date_time))
f.write("\n ------------------------------------------------------------- \n")
f.write("Current temperature is: {:.2f} deg C\n".format(txtlist[0]))
f.write("{},{} \n".format("Current weather desc :" ,txtlist[1]))
f.write("{},{},{} \n".format("Current Humidity :",txtlist[2],"%"))
f.write("{},{},{} \n".format("Current wind speed :",txtlist[3],"kmph"))
f.write("====================================================")