Parsing a string representing float * with exponent * in Python

I have a large file with numbers in the form of 6,52353753563E-7 . Thus, an exponent is present on this line. float() dies from this.

While I could write my own code to pre-process the string into something that float() might have, I'm looking for a Python way to convert them to float (something like a format string passed somewhere). I have to say that I am surprised that float() cannot handle strings with such an exponent, this is pretty common stuff.

I am using python 2.6, but 3.1 is an option if necessary.

+6
python
source share
2 answers

Nothing to do with the exhibitor. Task is a comma instead of a decimal point.

 >>> float("6,52353753563E-7") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for float(): 6,52353753563E-7 >>> float("6.52353753563E-7") 6.5235375356299998e-07 

For a general approach, see locale.atof ()

+13
source share

Your problem is not an exponent, but a comma. since python 3.1:

 >>> a = "6.52353753563E-7" >>> float(a) 6.52353753563e-07 
+1
source share

All Articles