I am an extremely novice Python user trying to sum columns of data in a .csv file. I found other answers that really helped me to get started (here and here, for example).
However, my problem is that I want to loop over my file to get sums for all columns.
My formatted data look like this:
z y x w v u
a 0 8 7 6 0 5
b 0 0 5 4 0 3
c 0 2 3 4 0 3
d 0 6 7 8 0 9
Or like this in .csv format:
,z,y,x,w,v,u
a,0,8,7,6,0,5
b,0,0,5,4,0,3
c,0,2,3,4,0,3
d,0,6,7,8,0,9
For right now, I am just trying to get the iteration to work. I will worry about the summing later. Here's my code:
import csv
data = file("test.csv", "r")
headerrow = data.next()
headerrow = headerrow.strip().split(",")
end = len(headerrow)
for i in range (1, end):
for row in csv.reader(data):
print row[i]
Here is what I get:
>>>
0
0
0
0
>>>
So, it prints the values at index 1 for each row, but does not continue through the other indices.
What obvious thing am I missing here?
Update:
Following the very helpful suggestions and explanations, I now have this:
import csv
with open("test.csv") as data:
headerrow = next(data)
delim = "," if "," == headerrow[0] else " "
headerrow = filter(None, headerrow.rstrip().split(delim))
reader = csv.reader(data, delimiter=delim, skipinitialspace=True)
zipped = zip(*reader)
print zipped
strings = next(zipped)
print ([sum(map(int,col)) for col in zipped])
This returns an error:
Traceback (most recent call last):
File "C:\Users\the hexarch\Desktop\remove_total_absences_test.py", line 9, in <module>
strings = next(zipped)
TypeError: list object is not an iterator
I do not understand this...? Sorry!