Summing a csv column in Python; issues with integers and strings

553 views Asked by At

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

1

There are 1 answers

1
fredtantini On

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 with float before converting to int:

>>> a='1.0'
>>> int(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.0'
>>> float(a)
1.0
>>> int(float(a))
1

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 add row[1] for each row, or initialize a list to [], then append each cell, and then use sum on this list.

>>> with open("f") as f:
...  c = csv.reader(f, delimiter=',')
...  some_var = 0
...  some_list = []
...  for r in c:
...   some_var += int(float(r[1]))
...   some_list.append(int(float(r[1])))
...
>>> some_var
6
>>> sum(some_list)
6