3

I'm currently working with a Telit module (GT864-py) and I'm trying to extract numbers from a return value/string that I recieve when using AT-commands.

This is an example of the code that I'm using:

MDM.send('AT#ADC=1,2'+'\r', 5)
pump = MDM.receive(15)
pumpb = int(filter(str.isdigit, pump))

which gives the response

#ADC: 10 (This number can range from ~10-150)
OK

Now, I would like to filter the number after ADC, however, I have not yet found a solution as to how.

By using this code in PythonWin 1.5.2+ I get the following error:

NameError: isdigit

So I am assuming isdigit isn't supported in Python 1.5.2, is that correct? And if so, does anyone know any other ways to extract the numbers after #ADC: xxx ?

6
  • 1
    Why are you using 1.5.2? You could write your own isdigit using all, I suppose. Commented Oct 19, 2015 at 11:23
  • I guess you will have more success if you contact Python's IRC channel if you want to find users that have experienced this ancient Python version first hand. Commented Oct 19, 2015 at 11:24
  • 2
    IIRC Python 1.5.2 already had regexps... But really, Python 1.5.2 ??? That was early 1999 or so ? Commented Oct 19, 2015 at 11:36
  • @jonrsharpe: all doesn't exist either :-) Commented Oct 19, 2015 at 11:38
  • 1
    The reason that I am using Python 1.5.2 is that the Telit module itself has a built-in python v 1.5.2 interpreter which, unfortunately, can't be updated to a later version. If it had a later version, say 2.7, this would have been a lot easier but now I'm left with trying to figure out a way to make it work on this old, obsolete piece of machinery Commented Oct 19, 2015 at 12:09

2 Answers 2

2

The Python 1.5.2p2 documentation is available online. Indeed, there is no isdigit in either str or in the module string.


Even in Python 1.5, str is a sequence that supports the in operation, so you could do:

def isdigit(c):
    return c in '0123456789'

pumpb = int(filter(isdigit, pump))

For more thorough parsing I'd use a regular expression, with module re instead; the code

import re
match = re.search('#ADC:\s*(\d+)', pump)
if match:
    number = match.group(1)

This will match #ADC: followed by any number of spaces, followed by 1 or more digits [0-9]; the digits are captured in the group 1, whose value is then stored to number if a match was found.

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

1 Comment

After initial testing, this seems to work perfectly.I will try it more extensively with different numbers later but for now, a big thanks to you Mr. Haapala
0

If the string is always exactly "#ADC: ", then simple string slicing should also work:

if pump[:6] == '#ADC: ':
    pumpb = int(pump[6:])

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.