Reputation: 91
I am currently writing a script to read two columns of float numbers in a CSV file and find the average of each column. I do not understand why my code is giving me a ValueError I/O operation on closed file.
My code has two open statements since to my understanding you must close the file and reopen it before adding and finding the average to a second column.
My code is below, I appreciate any feedback I can get on this, It is not making sense to me. Thank you.
Language: Python 3.6
def main():
import csv
file = input("What is the name of the file, dont forget the .csv: ")
INFILE = open(file,"r")
totalCol1 = 0
countCol1 = 0
totalCol2 = 0
countCol2 = 0
read = csv.reader(INFILE,lineterminator=",")
# Loop through column 1
for row in read:
totalCol1 += float(row[0])
countCol1 += 1
averageCol1 = totalCol1 / countCol1
INFILE.close()
INFILE = open(file,"r")
for row in read:
totalCol2 += float(row[1])
countCol2 += 1
averageCol2 = totalCol2 / countCol2
print('Average for Column One:%5.2f' % averageCol1)
print('Average for Column Two:%5.2f' % averageCol2)
INFILE.close()
main()
Upvotes: 1
Views: 76
Reputation: 36
I suspect what's happening is that you pass an instance of INFILE to csv.reader that then gets closed. So when you open the file again you need to pass that new instance to csv.reader.
That being said though, you can do all of this in the first loop without having to close and reopen the file:
for row in read:
totalCol1 += float(row[0])
countCol1 += 1
totalCol2 += float(row[1])
countCol2 += 1
averageCol1 = totalCol1 / countCol1
averageCol2 = totalCol2 / countCol2
or you could just use pandas read_csv to read the csv and then calculate the average using pandas mean and avoid looping (a worthwhile endeavor in Python).
Upvotes: 2