A.Rowden A.Rowden - 3 months ago 16
Python Question

Could not convert string to float: ...Sometimes

I'm having some strange things happen when converting strings to floats in a loop.

So when I use the following code exactly it writes:

[1468436874000, 0.00254071495719],
[1468528803000, 0.00341349353996],
[1468688596000, 0.000853373384991],
[1468871365000, 0.00256012015497],


It stops short, there should be about 30 lines more than that and those are the wrong calculations.

The function is:

def main():

minprice = pricelist('MIN')
maxprice = pricelist('MAX')
avgprice = pricelist('AVG')
avgsqft = sqftlist()
datelist = getdates()
index = fileparser()

with open('/home/andrew/Desktop/index3.htm', 'w') as file:

file.writelines(data[:index[0]])

for date, minprice, maxprice in zip(datelist, minprice, maxprice):
file.writelines('[%s, %s, %s],\n' % (date, minprice, maxprice))

file.writelines(data[index[1]:index[2]])

for date, avgprice in zip(datelist, avgprice):
file.writelines('[%s, %s],\n' % (date, avgprice))

file.writelines(data[index[3]:index[4]])

for date, avgprice, avgsqft in zip(datelist, avgprice, avgsqft):
file.writelines('[%s, %s],\n' % (date, ((float(avgprice))/(float(avgsqft)))))

file.writelines(data[index[5]:])
file.close()


The error is:

file.writelines('[%s, %s],\n' % (date, ((float(avgprice))/(float(avgsqft)))))
ValueError: could not convert string to float: .


Oddly, when I comment out the other for loops before it, the result is:

[1468436874000, 2.82644376127],
[1468528803000, 2.86702735915],
[1468688596000, 2.8546107764],
[1468871365000, 2.8546107764],
[1468871996000, 2.8546107764],
[1468919656000, 2.85383420662],
[1469004050000, 2.85189704903],
[1469116491000, 2.87361540168],
[1469189815000, 2.86059636119],
[1469276601000, 2.83694745621],
[1469367041000, 2.83903252711],
[1469547497000, 2.83848688853],
[1469649630000, 2.83803033196],
[1469736031000, 2.82327110329],
[1469790030000, 2.82650020338],
[1469876430000, 2.96552660866],
[1470022624000, 2.93407180385],


Moreover, when I use enumerate instead of zip (and make the appropriate changes), it works. I've examined both lists at the fifth item for anything unusual because that's where it's getting hung up, but there is nothing odd there in either list. Since it does work fine with enumerate I'll just do that for now. But I'm new to Python/programming in general and want to understand what exactly is causing this.

UPDATE Should have included this the first time.

# file.writelines(data[:index[0]+1])
# for date, minprice, maxprice in zip(datelist, minprice, maxprice):
# file.writelines('[%s, %s, %s],\n' % (date, minprice, maxprice))
# file.writelines(data[index[1]:index[2]+1])
# for date, avgprice in zip(datelist, avgprice):
# file.writelines('[%s, %s],\n' % (date, avgprice))
# file.writelines(data[index[3]:index[4]+1])
# time.sleep(1)
for date, avgprice, avgsqft in zip(datelist, avgprice, avgsqft):
# file.writelines(
print'[%s, %s],\n' % (date, ((float(avgprice))/(float(avgsqft))))
# file.writelines(data[index[5]:])
# file.close()


Prints... (correctly)

[1468436874000, 2.82644376127],

[1468528803000, 2.86702735915],

[1468688596000, 2.8546107764],

[1468871365000, 2.8546107764],

[1468871996000, 2.8546107764],

[1468919656000, 2.85383420662],

etc...

Answer

Debug by printing the values of avgprice and avgsqft in your code. You are getting some string as it's value which can not be converted to float