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. i.e.
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

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:
Second, keep the entire row:
To write the data to file: