5

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.

4
  • 1
    Are you saying that 0.000\t0.000...\t0.000\n is all one line? If so, then I'd assume that's what's being passed into float() and it can't convert. Seems like you'd need to split it up into individual float values. Is that the case? Commented Nov 2, 2011 at 19:52
  • @eldarerathis Yes it is all one line, but when I process it appears to make it through a certain number of values then gives me the error: b = map(float, lines) ValueError: invalid literal for float(): 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0. Is is not actually making it through the values? Commented Nov 2, 2011 at 19:54
  • That looks like just the one string you noted above, but with the \t being displayed as whitespace (i.e. an actual tab character). That's all one string literal, not a bunch of shorter ones, I'm guessing. Commented Nov 2, 2011 at 20:00
  • @eldarerathis That would make sense. It is simply looking at that as one string. Thanks for the help. Commented Nov 2, 2011 at 20:53

3 Answers 3

7

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')]
Sign up to request clarification or add additional context in comments.

Comments

2

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]]

1 Comment

Thanks for the help, this helps me understand my error. What I don't understand is how it appeared like some of the date was passed through the process before an error was noticed? Is this a misunderstanding on my part?
2

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]]

1 Comment

Thank you very much for the help. This makes what I was doing incorrectly very clear. I really appreciate the comments!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.