Python Invalid literal for float

I am running code to select chunks from a large file. I am getting a strange error that

"Invalid literal for float(): E-135"

      

Does anyone know how to fix this? Thanks in advance.

Actually this is the statement which gives me the error

float (line_temp[line(line_temp)-1]) 

      

This statement raises an error line_temp - line 'line' is any line in an open file as well as a line.

+2


source to share


4 answers


You need a number before E to make it a valid string representation of a floating point number

>>> float('1E-135')
1e-135
>>> float('E-135')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): E-135

      



In fact, what number is E-135 supposed to represent? 1x10^-135

?

Valid literals for floats are here .

+6


source


It looks like you are trying to convert a string to float. If it is a string E-135

, then this is indeed an invalid value that should be converted to float. Perhaps you are chopping off the digit at the beginning of the line and it really should be something like 1E-135

? This will be a valid float.



+3


source


May I suggest you replace

float(x-y)

      

from

float(x) - float(y)

      

+1


source


Ronald, kindly check the answers again. They are right. What are you doing: float (EXPRESSION) where the EXPRESSION result is E-135. E-135 is not valid for input to float () function. I don't know what "line_temp [line (line_temp) -1]" does, but it returns incorrect data for the float () function.

+1


source







All Articles