Hi all,

I seem to be having problems with summing all the values in a .csv file with the following format:

3.2323232312
7.5345345675
3.3423565624
etc...

I have opened this with a spreadsheet at it seems the floats are all in column[0] and each number is on a different row.

with open("E1.csv", "rb") as ins:
    for row in csv.reader(ins):
        print sum(map(int, row))

This gives the following error:
ValueError: invalid literal for int() with base 10: "0.2035..."

I would really appreciate some help, thanks.

When you iterate over with csv.reader(ins) you get a list output.
['3.2323232312']

>>> map(int, ['3.2323232312'])
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.2323232312'

>>> map(float, ['3.2323232312'])
[3.2323232312]

Even if you use float you just get 3 separate list,and sum() would not work.
If you want to add up those numbers,here one way.

with open('e1.csv') as ins:
    print sum(float(i) for i in ins) #14.1092143611

If you have more problem post an example of how E1.csv look,not just a column.
You dont have to post whole file,but some lines that are complete and desired output.

This test code might help ...

row = ['1.234', '4.563', '7.143']
print(row)  # test

# change list of strings to list of floats
row_float = map(float, row)
print(row_float)  # test

# change list of floats to list of integers
row_int = map(int, row_float)
print(row_int)  # test

# sum the integer list
sum_int = sum(row_int)
print(sum_int)

''' test result ...
['1.234', '4.563', '7.143']
[1.234, 4.563, 7.143]
[1, 4, 7]
12
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.