Python Forum
ValueError: invalid literal for int() with base 10: ''
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: invalid literal for int() with base 10: ''
#1
I have a Pyhon 3 application using a Maxbotix Ultrasonic Rangefinder (HR-USB-EZ1). 

Once in a great while I'll get the following error in my debug log and the script crashes. 
Error:
ValueError: invalid literal for int() with base 10: ''
The rangefinder returns data as a 'R + 4 numbers +'+ space + carriageReturn, eg 'R1234' <CR>. 
Very infrequently I get trashy data and have added some cleanup code.  I'm still getting the above error. 
I've avoided adding debug code that records the data as it would generate a lot of text for an error that seems to happen once every 4-5 days. 
From what I know I shouldn't get the error I'm getting, but apparently I'm missing something.  

clipped code:
valid="1234567890"
some stuff (flushing rangefinder, read RF data, etc)
data2=self.RF.readline()          # read the rangefinder
Rnge2 = str(data2)[str(data2).index('R')+1:str(data2).index('R')+5] #strips down to 4 numbers
Rnge2 = Rnge2.replace("'","")  #removes any single quotes
while True:
    if not set(Rnge2).issubset(valid):      #check for invalid chars - S/B only numerics
            Rnge = '9000'
            break
    if Rnge2 == '' :             #issubset won't catch this
            Rnge = '9000'
            break
    if int(Rnge2) > 1000 :          #I get the error here
            Rnge = '9000'
I was thinking of modifying
i[b]f Rnge2 =='':
to
if Rnge2 == '' or Rnge2==' ':
but the issubset statement should filter out spaces.

I keep looking at this and I just don't see it.  Anyone have some ideas?
Reply
#2
Actually, the check you are making is for 9000 is beyond the range of this sensor. The max is 5000 mm.


Quote:0px; "> Serial Output Format
The sensor output is provided over the COM port (or equivalent) in an ASCII character format. If a target is detected at
480 millimeters the output appears as follows: “R0480 <carriage return>”. The output is an ASCII capital “R”, followed
by four ASCII character digits representing the range in millimeters up to a maximum of 5000 millimeters. This is
followed by an ASCII space and a carriage return.

This from the pdf spec sheet at http://www.maxbotix.com/documents/HRUSB-...asheet.pdf
 There is also an interface sheet available at: http://www.maxbotix.com/articles/059.htm

Also, you can simplify your code if you convert the input to an integer as follows:
reading = int(data2[1:])
Larz60+
Reply
#3
I use the 9000 just as a flag that I'm beyond the range I'm interested in.  My only interest is if something has approached within a meter.   The exact distance isn't important for this application.

I keep it in a string as I've occasionally gotten junk in the output from the Maxbotix.    I've seen shortened output, zeros (R0000), etc.  I'm still getting occasional bad data, as the error I'm getting suggests.
Reply
#4
It's difficult to determine what junk is without examples.
Is it R with strange values afterwards, what does it look like?
(Example please)

Some things to try:
If you test it with Putty are seeing what you's expect?

Are all your settings correct --> Baud rate 57600, 8 data bits, 1 stop bit, no parity, no control (DTR, DSR, etc.)
Reply
#5
Like I stated in my post. The error occurs very infrequently, once every 3-4 days. I posted here in hopes of determining what's wrong with my code without collecting masses of data for several days in hope of finding the actual data.

Looking at the error it seems to be telling me that at line 14 Rnge2 is empty. Is that a correct interpretation?

If so, why isn't line 11 catching it?
Reply
#6
I can't think of anything that would result in what you've described. Could you just catch the ValueError and break out of the loop in that case?
Reply
#7
Yes, I could do that.  I guess I am a bit fixated on issubset as it seems it should filter any junk.  I'm starting to suspect that there's a control character sneaking in and issubset isn't handling it properly (IMO).  I've already determined a zero length string isn't handled properly (again, IMO) by issubset.  Perhaps instead of:
    if not set(Rnge2).issubset(valid):      #check for invalid chars - S/B only numerics
            Rnge = '9000'
            break
    if Rnge2 == '' :             #issubset won't catch this
            Rnge = '9000'
            break
I do:
    if not Rnge2.isnumeric():     #filter zero length and any non numeric char
            Rnge = '9000'
            break
Reply
#8
If you're intent on not finding the string that is actually causing the problem, I would use a regular expression instead:

import re
regex = re.compile('R(\d{4,4})')
some stuff (flushing rangefinder, read RF data, etc)
data2=self.RF.readline()          # read the rangefinder
match = regex.search(data2)
while True:
   if not match:      #check for invalid chars - S/B only numerics
           Rnge = '9000'
           break
   if int(match.groups(1)[0]) > 1000 :          
           Rnge = '9000'
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
It's not that I'm intent on not finding the problem string, it is that I've found the data I'm getting doesn't always follow the format Maxbotix specifies. I've seen results like 'R0000', 'RR1234', 'R324''(2 single quotes), etc. I even get inconsistent readings, ie R0899 then R4309 when the second read was milliseconds from the 1st. The posted code is just a snippet. The full code actually does 2 reads and if there's a greater than 10% difference I assume one is a bad read and wait for the next iteration to test for <1000 mm.

The hardware isn't 100% reliable. That may be caused by poor design, be it the manufacturer or the chassis it is installed in. Regardless, my task is to make it reliable.

In addition to the isnumeric I've added a few try/excepts to lock this down.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: invalid literal for int() with base 10: omega_elite 5 5,679 Dec-30-2020, 06:11 AM
Last Post: delonbest
  invalid literal for int() with base 10: '# NRECS: 1096\n' Baloch 8 4,459 May-24-2020, 02:08 AM
Last Post: Larz60+
  invalid literal for int() with base 10: '' mrsenorchuck 5 5,331 Apr-29-2020, 05:48 AM
Last Post: markfilan
  ValueError: invalid literal for int() with base 10: '\n' srisrinu 9 5,582 Apr-13-2020, 01:30 PM
Last Post: ibreeden
  zlib decompress error: invalid code lengths set / invalid block type DreamingInsanity 0 6,744 Mar-29-2020, 12:44 PM
Last Post: DreamingInsanity
  input-ValueError: invalid literal for int() jacklee26 2 2,511 Feb-21-2020, 01:27 PM
Last Post: ndc85430
  ValueError: invalid literal for int() with base 10: '0.5' emmapaw24 2 3,673 Feb-16-2020, 07:24 PM
Last Post: emmapaw24
  ValueError: invalid literal for int() with base 10: '' Jay123 7 7,219 Aug-05-2019, 02:43 PM
Last Post: Jay123
  ValueError: invalid rectstyle object fen1c5 1 5,620 Jun-05-2019, 02:51 PM
Last Post: heiner55
  ValueError: invalid literal for int() with base 10: '' ivinjjunior 6 9,069 Apr-20-2019, 05:37 PM
Last Post: keames

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020