I am new to python and I am trying to help my son with a school project. He needs to look at a csv with min and max outdoor temperature, and then write to a different CSV with the min temperature and the date that it occurred, and then do the same with the max temp. ie
7/4/1996 98
2/15/1921 -25.
I have figured out how to find the min and max and make it write it to a CSV, but I don't know how to get it to tag the min and max with the date that they occurred.
I used code from another post to get me started and made a few tweaks to it to get it to work, but don't know how to get the date with the temps. book.csv
is the file with the temperatures and dates. ook.output.csv
is the file I am outputting the data to.
Conceptually, I would think that if you could tag the line that the min came from in the CSV, and then use that somehow to make python write it to the same line as it puts the min, that would work. Just don't know how to do that.
import csv
mydelimeter = csv.excel()
mydelimeter.delimiter=","
myfile = open(r"C:\VAF\Cade\book.csv")
myfile.readline()
myreader=csv.reader(myfile,mydelimeter)
mywind,mydate=[],[]
minTemp, maxTemp = [],[]
for row in myreader:
minTemp.append(row[1])
maxTemp.append(row[2])
print ("Coldest temperature : ", min(minTemp))
print ("Hottest temperature : ", max(maxTemp))
data = [min(minTemp), max(maxTemp)]
with open("C:\VAF\Cade\ook.output.csv", 'w') as f:
writer = csv.writer(f)
writer.writerow(data)
CSV with made up data
output CSV with min and max temp
I just tested and it looks like the call to myfile.readline()
is what is causing this. It would appear that method reads the first line (line 0, the headers) and returns to nothing (since you aren't assigning the output to any variable).
That is why when you loop it starts off at line 1 not line 0.
Something like this is a little more succinct, and should help you accomplish what you're after.
import csv
with open('book.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter = ',')
for row in csv_reader:
print(row)
Two things to do here.
First, the values read by csv reader are strings not numbers so the min, max functions don't do numerical comparison.
Here's a possible way to read them into numbers:
rows = []
for row in myreader:
rows.append([row[0], int(row[1]), int(row[2])])
Second, keep the entire row:
min_temp = min(rows, key = lambda x: x[1])
max_temp = max(rows, key = lambda x: x[2])
To write the data to file:
data = [['date', 'temp'], [min_temp[0], min_temp[1]], [max_temp[0], max_temp[2]]]
with open("C:\VAF\Cade\ook.output.csv", 'w') as f:
writer = csv.writer(f)
for row in data:
writer.writerow(row)
To do what you want, you need to keep track of the dates that had the lowest and highest temperature. An easy way to do that is to remember it when updating the minimum or maximum temperatures that have been seen so far as each row of the file is processed.
Below is code that does this and also shows some better techniques for handling both files in general, and CSV files in particular.
import csv
inpfile = r"book.csv"
outpfile = r"book.output.csv"
my_dialect = csv.excel()
my_dialect.delimiter = ","
with open(inpfile, newline='') as myfile:
myreader = csv.reader(myfile, my_dialect)
next(myreader) # Skip header row.
row = next(myreader) # Read first data row.
# Initialize overall stats with data from it.
mindate = maxdate = row[0]
file_mintemp, file_maxtemp = int(row[1]), int(row[2])
# Process the rest of the rows.
for row in myreader:
row_mintemp, row_maxtemp = int(row[1]), int(row[2])
if row_mintemp < file_mintemp:
file_mintemp = row_mintemp
mindate = row[0]
if row_maxtemp > file_maxtemp:
file_maxtemp = row_maxtemp
maxdate = row[0]
print("Coldest temperature: ", file_mintemp)
print("Hottest temperature: ", file_maxtemp)
with open(outpfile, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow([mindate, file_mintemp])
writer.writerow([maxdate, file_maxtemp])
print('fini')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.