21

I've a script which reads temperature data:

def get_temp(socket, channels): data = {} for ch in channels: socket.sendall('KRDG? %s\n' % ch) time.sleep(0.2) temp = socket.recv(32).rstrip('\r\n') data[ch] = float(temp) 

Sometimes, the script fails on the line which converts the values to float:

File "./projector.py", line 129, in get_temp
data[ch] = float(temp)
ValueError: invalid literal for float(): +135.057E+0
+078.260E+0
+00029

but this is NOT an invalid literal. If I enter this into any python shell,

float(+135.057E+0) 

then it correctly returns 135.057.

So what is the problem?

5
  • 1
    literals are strings, so you're actually doing float('+135.057E+0'), not float(+135.057E+0), which of course works since it's already a float. but that conversion from string works too. Commented Feb 21, 2014 at 19:53
  • 8
    Is it possible it has some sort of invisible spurious character in it? Commented Feb 21, 2014 at 19:54
  • 6
    You can use print(repr(temp)) to check for non-printing characters. Commented Feb 21, 2014 at 19:55
  • 1
    As an example, float('+135.057E+0\0') fails with the same error message. Commented Feb 21, 2014 at 19:55
  • 1
    @g.d.d.c: You should put that in an answer; I think it's a better answer than mine. Commented Feb 21, 2014 at 19:59

3 Answers 3

15

I would all but guarantee that the issue is some sort of non-printing character that's present in the value you pulled off your socket. It looks like you're using Python 2.x, in which case you can check for them with this:

print repr(temp) 

You'll likely see something in there that's escaped in the form \x00. These non-printing characters don't show up when you print directly to the console, but their presence is enough to negatively impact the parsing of a string value into a float.

-- Edited for question changes --

It turns this is partly accurate for your issue - the root cause however appears to be that you're reading more information than you expect from your socket or otherwise receiving multiple values. You could do something like

map(float, temp.strip().split('\r\n')) 

In order to convert each of the values, but if your function is supposed to return a single float value this is likely to cause confusion. Anyway, the issue certainly revolves around the presence of characters you did not expect to see in the value you retrieved from your socket.

Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar issue reading the serial output from a digital scale. I was reading [3:12] out of a 18 characters long output string.

In my case sometimes there is a null character "\x00" (NUL) which magically appears in the scale's reply string and is not printed.

I was getting the error:

> ' 0.00' > 3 0 fast loop, delta = 10.0 weight = 0.0 > ' 0.00' > 1 800 fast loop, delta = 10.0 weight = 0.0 > ' 0.00' > 6 0 fast loop, delta = 10.0 weight = 0.0 > ' 0\x00.0' > Traceback (most recent call last): > File "measure_weight_speed.py", line 172, in start > valueScale = float(answer_string) > ValueError: invalid literal for float(): 0 

After some research I wrote few lines of code that work in my case.

replyScale = scale_port.read(18) answer = replyScale[3:12] answer_decode = answer.replace("\x00", "") answer_strip = str(answer_decode.strip()) print(repr(answer_strip)) valueScale = float(answer_strip) 

The answers in these posts helped:

  1. How to get rid of \x00 in my array of bytes?
  2. Invalid literal for float(): 0.000001, how to fix error?

Comments

0

Watch out for possible unintended literals in your argument

for example you can have a space within your argument, rendering it to a string / literal:

float(' 0.33') 

After making sure the unintended space did not make it into the argument, I was left with:

float(0.33) 

Like this it works like a charm.

Take away is: Pay Attention for unintended literals (e.g. spaces that you didn't see) within your input.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.