3

I use pySerial to read data from serial port and I am trying to convert the string below to integer:

s=ser.read()
int(s) 

but i can't.The error i take is:

Traceback (most recent call last):
File "C:\Documents and Settings\User\Desktop\ser1.py", line 24, in <module>
int(s)
ValueError: invalid literal for int() with base 10: '' 

Do anyone know how to convert it? Thanks in advance!

2
  • 4
    And what number are you expecting to get when converting an empty string? Commented Apr 30, 2013 at 12:22
  • It is supposed that reads the serial port,so it won't be empty.Can you suggest me any solution? Commented Apr 30, 2013 at 12:31

3 Answers 3

13
int('0'+s)

Prepend the string with a zero. Think of it as a miniature parsing step. I can't think of a case when this wouldn't work.

This is the use-case I had: Convert a numpy array of strings to integers.

def _intStrArray(pos): return int('0'+pos)
np.intStrArray = np.vectorize(_intStrArray)

 

print(arr)

 

array([['', '', '', '', '', '', '', '', '', '', '', '', ''],
       ['', '', '1', '', '', '2', '', '2', '', '', '', '', ''],
       ['', '2', '2', '', '3', '2', '', '', '2', '', '', '', ''],
       ['', '2', '', '3', '2', '2', '3', '4', '', '4', '', '', ''],
       ['', '', '3', '2', '2', '', '', '3', '3', '', '2', '1', ''],
       ['', '', '', '1', '2', '', '3', '2', '', '', '', '', ''],
       ['', '', '2', '', '2', '4', '3', '', '2', '1', '', '2', ''],
       ['', '', '', '', '', '', '2', '', '', '1', '', '1', ''],
       ['', '', '', '', '', '', '', '', '', '', '', '', '']], dtype=object)

 

print(np.intStrArray(arr))

 

array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0],
       [0, 2, 2, 0, 3, 2, 0, 0, 2, 0, 0, 0, 0],
       [0, 2, 0, 3, 2, 2, 3, 4, 0, 4, 0, 0, 0],
       [0, 0, 3, 2, 2, 0, 0, 3, 3, 0, 2, 1, 0],
       [0, 0, 0, 1, 2, 0, 3, 2, 0, 0, 0, 0, 0],
       [0, 0, 2, 0, 2, 4, 3, 0, 2, 1, 0, 2, 0],
       [0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 1, 0],
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

After discovering just how messy my data was, I also added a str(). This is optional if the datatype is reliable.

def _intStrArray(pos): return int('0'+str(pos))
np.intStrArray = np.vectorize(_intStrArray)
Sign up to request clarification or add additional context in comments.

2 Comments

It would probably be more appropriate to convert an empty string to None instead of 0.
it doesn't work for negative integers
6

You could catch the exception and default the value to something, but I really don't see there's anything meaningful from something that doesn't exist...

s = ser.read()
try:
    ival = int(s)
except ValueError as e:
    ival = 0 # ????

It would be more likely that the data you're expecting just hasn't been received yet and you should be waiting on the serial port until sufficient data is received before doing anything with it...

Comments

-2

int(x=0) int(x, base=10)

Convert a number or string x to an integer, or return 0 if no arguments are given. If x is a number, it can be a plain integer, a long integer, or a floating point number. If x is floating point, the conversion truncates towards zero. If the argument is outside the integer range, the function returns a long object instead.

If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in radix base. Optionally, the literal can be preceded by + or - (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2-36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O/0, or 0x/0X, as with integer literals in code. Base 0 means to interpret the string exactly as an integer literal, so that the actual base is 2, 8, 10, or 16.

2 Comments

It's not helpful to simply quote huge chunks of documentation, especially without marking it as such.
You can create a function with exception declaration return a default value. def num (s): try: return int(s) except exceptions.ValueError: return 0

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.