Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python Invalid literal for float

Tags:

python

I am running a code to select chunks from a big file. I am getting some strange error that is

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

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

Actually this is the statement that is giving me error

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

This statement produces error line_temp is a string 'line' is any line in an open and file also a string.


2 Answers

You need a number in front of the E to make it a valid string representation of a float 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, which number is E-135 supposed to represent? 1x10^-135?

Valid literal forms for floats are here.

like image 65
Vinko Vrsalovic Avatar answered May 02 '26 19:05

Vinko Vrsalovic


Looks like you are trying to convert a string to a float. If the string is E-135, then it is indeed an invalid value to be converted to a float. Perhaps you are chopping off a digit in the beginning of the string and it really ought to be something like 1E-135? That would be a valid float.

like image 41
Arkady Avatar answered May 02 '26 19:05

Arkady