Reputation: 33
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
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