Edwin Regalado
Edwin Regalado

Reputation: 33

csv parsing and adding floats is returning a casting error

I am parsing through a csv file and pulling strings that can be turned into floats. When converting them I'm getting the right data type but I keep getting this error on my addition line. I have no idea as to why

    total = 0.0;
    for line in csv_reader:
      if(line[0] == 'Jill'):
        total += float(line[3]);
    print(total);

i am getting the following error

Traceback (most recent call last):
  File "/Users/edwinregalado/Desktop/FarmersFridge/parseCSV.py", line 22, in <module>
    total += float(line[3]);
ValueError: could not convert string to float: 

Upvotes: 0

Views: 55

Answers (1)

d_v_t_
d_v_t_

Reputation: 56

The error is likely that you are trying to read a string that doesn't convert nicely into a float (2.2.0 for example). Try printing line[3] before executing the casting and addition. The last print output before an error should tell you what is not being cast correctly and often times, it's pretty obvious why casting of this value fails.

Upvotes: 1

Related Questions