what is the best way to match numbers in python
what I trying to do?
I am reading a serial connection, the output is of the range 0 – 1023.
sometimes however I get the following string (they are read as ‘str’):
1023
10?1023
1023
I am matching the output to the alphabet.
the moment I am using:
input = '10?1023'
print sum(int(x) for x in re.findall(r'\d+', input))
as I thought a generator would be quicker than list comprehension.
But matching would give me 1033. Ideally I would like it to return none.
any ideas?, I think I just need to work on the regrep syntax. Is there a way of excluding sum, when using generators?
If I understand correctly, you want to cast a string to an integer, except you want
Noneif it isn’t one.Or if you want to sum all the integers on a single line: