I'm trying to convert this list of numbers (that are strings) into floats so I can do an analysis with them. However, I receive an error when trying to convert them to float.
This is the list:
temp_gallons = ['404,443.60 ', '367,223.12 ', '388,369.08 ', '352,472.56 ', '386,618.76 ', '333,929.64 ', '326,868.52 ', '257,663.56 ', '0.00 ', '0.00 ', '0.00 ', '0.00 ', '2,817,589 ']
This is the for loop iteration for adding them while temporarily converting them into a float:
Q1_Gallons = 0.0
for i in range(0,2):
Q1_Gallons = Q1_Gallons + float(temp_gallons[i])
I figured the reason was because there is a white space at the end of each number. So I tried stripping the values using a for loop iteration:
for i in temp_gallons:
i.strip(" ")
However, the list remains the same after the strip and I still cannot convert the string to float and/or add the numbers.
Just for context: these are values for each month and I am making a quarterly savings analysis adding the values for the first three months, next three, and so forth.
float(' \t 404443.60 \n ')evaluates to404443.6.