[docs] floating point conversion error

Brian D'Urso dursolab at gmail.com
Fri Jul 6 21:57:23 CEST 2012


Converting from a string to a float (for example, after having read in data
from a file) encounter a "value error: invalid literal" error when given
'00E', which is part of '00E+00', the representation used by several
spreadsheet programs for 0 when a cell is set to scientific notation. This
renders all python programs unable to work with any data input from files
created by a program that adheres to this convention, most notably EXCEL
and Calc, without specific user intervention, either on the spreadsheet or
pythons sides. his should properly translate to 0.0 as a float when
encountered.

Example code:

#expects data of this form (a CSV file):
        #,,Height Comparison (mm),,,Tilt Comparison (degrees),,,

#6,1.2,0.260936375,0.246329669,0.014606706,0.147779582,2.70248E-10,c,asdfj,
234,,

#7,1.4,0.30234927,0.287927142,0.014422127,1.042261298,1.45951E-08,cdv128,sgaoda,dga,kjadodf,,

#8,1.4,0.245442271,0.308505346,-0.063063075,0.745239509,0,1234891,13E-89,sdfglksj,,

dat =
re.findall("([-]?[\.\d]*),([-]?[\.\d]*),([-]?[\.\d]*),([-]?[\.\d]*),[-]?[\.\d]*,([-]?[\.\d]*),([-]?[\.\d]*[E]?[-]?[\.\d]*)",info)
    for x in dat:

stuff=[float(x[0]),float(x[1]),float(x[2]),float(x[3]),float(x[4]),float(x[5])]
#will fail here if any value is 00E+00
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20120706/00bf6fa2/attachment.html>


More information about the docs mailing list