Reputation: 1
user_api = os.environ['Api_key']
location = input("Vpišite ime mesta: ")
def Vremenska_napoved():
"""Za kraj, ki ga uporabnik vnese mu funkcija izpiše stanje vremena za tisti trenutek"""
complete_api_link = "https://api.openweathermap.org/data/2.5/weather?q="+location+"&appid="+user_api
api_link = requests.get(complete_api_link)
api_data = api_link.json()
if api_data ['cod'] == '404':
print("Mesto ne obstaja: {}, Preverite pravilni vnos mesta za katerega želite vremensko napoved".format(location))
else:
#naredimo spremenljivke za shranjevanje in prikaz podatkov o vremenu
Temperatura = ((api_data['main']['temp']) - 273.15)
Vlažnost = api_data['main']['humidity']
Hitrost_vetra = api_data['wind']['speed']
Občuti_se_kot = api_data['weather'][0]['description']
Datum_čas = datetime.now().strftime("%d %b %Y | %I:%M:%S %p")
print ("_____________________________________________________________")
print ("Vremenska napoved za - {} || {}".format(location.upper(), Datum_čas))
print ("_____________________________________________________________")
print ("Trenutna temperatura: {:.2f} stopinj C".format(Temperatura))
print ("Trenutna vlažnost:",Vlažnost, '%')
print ("Trenutna hitrost vetra:",Hitrost_vetra ,'km/h')
print ("Občuti se kot:",Občuti_se_kot)
Vremenska_napoved()
Weather in PODBREZJE || 03 May 2021 | 07:13:32 PM
Temp: 12.54 stopinj C Hum: 54 % W_speed: 3.09 km/h
So this is my code and the output given by python and I am wondering how I could save Temp, Hum and W_speed into the CSV file, because I want to collect weather data for 1 day and process it.
Upvotes: 0
Views: 196
Reputation: 108
Here you have the answer!
The data is stored in a dictionary and a csv is created appending the new data. If the file does not exists, the headers are added to the file.
I haven't tried the solution but the 'pseudocode' could help you with your requirements!
import os
import csv
user_api = os.environ['Api_key']
location = input("Vpišite ime mesta: ")
def Vremenska_napoved():
"""Za kraj, ki ga uporabnik vnese mu funkcija izpiše stanje vremena za tisti trenutek"""
complete_api_link = "https://api.openweathermap.org/data/2.5/weather?q="+location+"&appid="+user_api
api_link = requests.get(complete_api_link)
api_data = api_link.json()
if api_data ['cod'] == '404':
print("Mesto ne obstaja: {}, Preverite pravilni vnos mesta za katerega želite vremensko napoved".format(location))
else:
#naredimo spremenljivke za shranjevanje in prikaz podatkov o vremenu
Temperatura = ((api_data['main']['temp']) - 273.15)
Vlažnost = api_data['main']['humidity']
Hitrost_vetra = api_data['wind']['speed']
Občuti_se_kot = api_data['weather'][0]['description']
Datum_čas = datetime.now().strftime("%d %b %Y | %I:%M:%S %p")
headers = ['Date', 'Temp', 'Hum', 'W_speed']
filename = 'output.csv'
file_exists = os.path.isfile(filename)
row = {
'Date': Datum_čas
'Temp': Temperatura,
'Hum': Vlažnost,
'W_speed': Hitrost_vetra
}
a_file = open(filename, "a")
dict_writer = csv.DictWriter(a_file, headers)
if not file_exists:
dict_writer.writeheader() # file doesn't exist yet, write a header
dict_writer.writerow(row)
a_file.close())
print ("_____________________________________________________________")
print ("Vremenska napoved za - {} || {}".format(location.upper(), Datum_čas))
print ("_____________________________________________________________")
print ("Trenutna temperatura: {:.2f} stopinj C".format(Temperatura))
print ("Trenutna vlažnost:",Vlažnost, '%')
print ("Trenutna hitrost vetra:",Hitrost_vetra ,'km/h')
print ("Občuti se kot:",Občuti_se_kot)
Vremenska_napoved()
Upvotes: 2