Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: invalid literal for float() in Python

To all:

I have curious if someone can help me understand the error: ValueError: invalid literal for float(). I am getting this when I am passing a text file to a list then trying to convert this list to float values.

a = open("input.txt","r")
lines = a.readlines()
b = map(float, lines)

What is odd, at least to me is that when I process:

print repr(lines[0])

I get:

'0.000\t0.000...\t0.000\t0.000\n'

and

print type(lines[0])

I get:

<type 'str'>

I don't understand therefore why the map(float, lines) does not work correctly. Am I using this function incorrectly? Looking at the documentation the map function is given as: map(function, iterable, ...). Is a list not iterable?

Also if someone could explain this error/point me in the direction of an explanation for this error I would greatly appreciate it.

Thanks in advance for help with this question.

like image 725
geop Avatar asked Nov 02 '11 19:11

geop


People also ask

How do I fix invalid literal in Python?

The Python ValueError: invalid literal for int() with base 10 error is raised when you try to convert a string value that is not formatted as an integer. To solve this problem, you can use the float() method to convert a floating-point number in a string to an integer.

What is an invalid literal in Python?

The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit.

How do I fix ValueError invalid literal for int <UNK> with base 10 in python?

The Python "ValueError: invalid literal for int() with base 10" occurs when we pass a string that cannot be directly converted to an integer (e.g. an empty string or a float) to the int() class. To solve the error, convert the string to a float first, e.g. int(float(my_str)) .


3 Answers

You don't need readlines in this case -- it's a waste of time and memory.

If you want a list of lists of floats:

b = [[float(v) for v in line.rstrip('\n').split('\t')] for line in a]

or just one big list of floats:

b = [float(v) for line in a for v in line.rstrip('\n').split('\t')]
like image 99
John Machin Avatar answered Sep 20 '22 08:09

John Machin


The ValueError is coming from the \t character in the string. You must split each line into the individual columns before converting each one individually.

>>> lines = ['0.000\t1.000\t2.000\n', '3.000\t4\t5.0\n']
>>> [[float(val) for val in line.strip().split('\t')] for line in lines]
[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0]]
like image 40
Michael Merickel Avatar answered Sep 17 '22 08:09

Michael Merickel


a.readlines() is a list of strings, so you're trying to convert float('0.000\t0.000\t0.000\t0.000\n') in your map, which explains the error you're seeing.

You need to do a bit more processing (see the inline comments):

>>> x = '0.000\t0.000\t0.000\t0.000\n'
# To simulate a.readlines()' list
>>> lines = [x,]
>>> 

# Strip the newline, and separate the values based on the tab control character.
>>> lines_values = map(lambda l: l.strip().split('\t'), lines)
>>> lines_values
[['0.000', '0.000', '0.000', '0.000']]

# For each value in in the list of lines' values, convert from string to a float.
>>> values_float = [map(float, v) for v in values]
>>> values_float
[[0.0, 0.0, 0.0, 0.0]]
like image 22
Sam Dolan Avatar answered Sep 21 '22 08:09

Sam Dolan