I have read a csv file in using reader and need to get the total of each column. I've followed a few threads on stackexchange and have got the following code:
with open ("column_formatted.csv") as csvfile2:
csv_f2 = csv.reader(csvfile2, delimiter=',')
for row in csv_f2:
print row[1]
print sum(row[1])
The output of the first print statement shows the data is this:
43.0
60.0
10.0
But the print & sum function gives the following error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'.
I tried converting to integer with the following:
print sum(int(row[1]))
but this time I get the error:
ValueError: invalid literal for int() with base 10: '43.0'
I'm not sure what to do to with this information to allow me to get the total for each column. Any help would be really great, Thanks
I am not really familiar with the csv module, but I think that your problem is that
row[1]
is indeed a string. You could work withfloat
before converting toint
:But then
sum(int(float(row[1])))
will fail since 'int' objects are not iterable. You may want to initialize an int to 0, and addrow[1]
for each row, or initialize a list to [], then append each cell, and then use sum on this list.