get data from multiple csv file and print Highest, Lowest day weather with Humid from any year and also print month name and day in python
Question:
Hi everyone. I have multiple CSV files I am creating a weatherman app in python. I am getting data from CSV files and here is the code
import os
import csv
lst_temp = []
lst_hum = []
dates = []
class Weather:
def main(self):
path = r'C:UserssomeonePycharmProjectsuntitledweatherfiles\'
os.system('cls')
for files in os.listdir(path):
if files.endswith('.txt'):
with open(path + files, 'r') as weather:
input_file = csv.reader(weather)
for row in input_file:
date = row[0].split('-')
if date[0] == '2013':
lst_temp.append(row[1])
lst_hum.append(row[7])
lst_temp_int = [int(i) for i in lst_temp if i]
lst_hum_int = [int(i) for i in lst_hum if i]
sorted_lst = sorted(lst_temp_int)
sorted_hum_lst = sorted(lst_hum_int)
print(f"Highest: {sorted_lst[-1]}C")
print(f"Lowest: {sorted_lst[0]}C")
print(f"Humid: {sorted_hum_lst[-1]}%")
they are giving me data in this format
Highest: 70C
Lowest: -1C
Humid: 100%
I need the result in this format
Highest: 45C on June 23
Lowest: 01C on December 22
Humid: 95% on August 14
can anyone help me I am very grateful for this? thank you
Answers:
You might want to use pandas to parse data files.
Assuming the column names are the same throughout your .txt files:
import pandas as pd
data = pd.read_csv(filepath, sep=',', parse_dates=['PKT'])
After that, you can retrieve the index of the max temperature using .idxmax() like so:
max_i = df['Max TemperatureC'].idxmax()
max_temp_row = df.iloc[max_i]
Or the minimum temperature using .idxmin()
min_i = df['Max TemperatureC'].idxmin()
min_temp_row = df.iloc[max_i]
I highly recommend you read the pandas documentation for more info.
from datetime import datetime
import csv
import os
temperatures = []
temperatures_min = []
class Weather:
def Year_Temp(self):
path = r'C:UserssomeonePycharmProjectsuntitledweatherfiles\'
year = self.func
os.system('cls')
for files in os.listdir(path):
if files.endswith('.txt'):
with open(path + files, 'r') as weather:
input_file = csv.reader(weather)
next(input_file)
for line in input_file:
if year in line[0]:
date = line[0]
temp_max = line[1]
temp_min = line[3]
humd = line[8]
temperatures.append(temp_max)
dates.append(date)
temperatures_min.append(temp_min)
lst_hum.append(humd)
res = dict(zip(temperatures, dates))
new_dict = {k: v for k, v in res.items() if k != ''}
max_temp = max(new_dict, key=int)
strpt_date = new_dict.get(max_temp)
mydate = datetime.strptime(strpt_date, '%Y-%m-%d')
strf_date = mydate.strftime('%B %d')
print(f"Highest: {max_temp}C, {strf_date}")
res_one = dict(zip(temperatures_min, dates))
new_dict_one = {k: v for k, v in res_one.items() if k != ''}
min_temp = min(new_dict_one, key=int)
strpt_date_one = new_dict_one.get(min_temp)
mydate_one = datetime.strptime(strpt_date_one, '%Y-%m-%d')
strf_date_one = mydate_one.strftime('%B %d')
print(f"Lowest: {min_temp}C, {strf_date_one}")
res_two = dict(zip(lst_hum, dates))
new_dict_two = {k: v for k, v in res_two.items() if k != ''}
max_humd = max(new_dict_two, key=int)
strpt_date_two = new_dict_two.get(max_humd)
mydate_two = datetime.strptime(strpt_date_two, '%Y-%m-%d')
strf_date_two = mydate_two.strftime('%B %d')
print(f"Humid: {max_humd}%, {strf_date_two}")
your Wellcome
Hi everyone. I have multiple CSV files I am creating a weatherman app in python. I am getting data from CSV files and here is the code
import os
import csv
lst_temp = []
lst_hum = []
dates = []
class Weather:
def main(self):
path = r'C:UserssomeonePycharmProjectsuntitledweatherfiles\'
os.system('cls')
for files in os.listdir(path):
if files.endswith('.txt'):
with open(path + files, 'r') as weather:
input_file = csv.reader(weather)
for row in input_file:
date = row[0].split('-')
if date[0] == '2013':
lst_temp.append(row[1])
lst_hum.append(row[7])
lst_temp_int = [int(i) for i in lst_temp if i]
lst_hum_int = [int(i) for i in lst_hum if i]
sorted_lst = sorted(lst_temp_int)
sorted_hum_lst = sorted(lst_hum_int)
print(f"Highest: {sorted_lst[-1]}C")
print(f"Lowest: {sorted_lst[0]}C")
print(f"Humid: {sorted_hum_lst[-1]}%")
they are giving me data in this format
Highest: 70C
Lowest: -1C
Humid: 100%
I need the result in this format
Highest: 45C on June 23
Lowest: 01C on December 22
Humid: 95% on August 14
can anyone help me I am very grateful for this? thank you
You might want to use pandas to parse data files.
Assuming the column names are the same throughout your .txt files:
import pandas as pd
data = pd.read_csv(filepath, sep=',', parse_dates=['PKT'])
After that, you can retrieve the index of the max temperature using .idxmax() like so:
max_i = df['Max TemperatureC'].idxmax()
max_temp_row = df.iloc[max_i]
Or the minimum temperature using .idxmin()
min_i = df['Max TemperatureC'].idxmin()
min_temp_row = df.iloc[max_i]
I highly recommend you read the pandas documentation for more info.
from datetime import datetime
import csv
import os
temperatures = []
temperatures_min = []
class Weather:
def Year_Temp(self):
path = r'C:UserssomeonePycharmProjectsuntitledweatherfiles\'
year = self.func
os.system('cls')
for files in os.listdir(path):
if files.endswith('.txt'):
with open(path + files, 'r') as weather:
input_file = csv.reader(weather)
next(input_file)
for line in input_file:
if year in line[0]:
date = line[0]
temp_max = line[1]
temp_min = line[3]
humd = line[8]
temperatures.append(temp_max)
dates.append(date)
temperatures_min.append(temp_min)
lst_hum.append(humd)
res = dict(zip(temperatures, dates))
new_dict = {k: v for k, v in res.items() if k != ''}
max_temp = max(new_dict, key=int)
strpt_date = new_dict.get(max_temp)
mydate = datetime.strptime(strpt_date, '%Y-%m-%d')
strf_date = mydate.strftime('%B %d')
print(f"Highest: {max_temp}C, {strf_date}")
res_one = dict(zip(temperatures_min, dates))
new_dict_one = {k: v for k, v in res_one.items() if k != ''}
min_temp = min(new_dict_one, key=int)
strpt_date_one = new_dict_one.get(min_temp)
mydate_one = datetime.strptime(strpt_date_one, '%Y-%m-%d')
strf_date_one = mydate_one.strftime('%B %d')
print(f"Lowest: {min_temp}C, {strf_date_one}")
res_two = dict(zip(lst_hum, dates))
new_dict_two = {k: v for k, v in res_two.items() if k != ''}
max_humd = max(new_dict_two, key=int)
strpt_date_two = new_dict_two.get(max_humd)
mydate_two = datetime.strptime(strpt_date_two, '%Y-%m-%d')
strf_date_two = mydate_two.strftime('%B %d')
print(f"Humid: {max_humd}%, {strf_date_two}")
your Wellcome