Pythonic way to parse a number out of a string that has a postfix

700 views Asked by At

Using Python 3.

I have a string such as 128kb/s, 5mb/s, or something as simple as 42!. There's no space between the numeric characters and its postfix, so I can't just invoke int(text) directly.

And I just want to capture the values of 128,5, and 42 into an integer.

At the moment, I just wrote a helper function that accumulates all the numbers into a string and breaks on the first non-numeric character.

def read_int_from_string(text):
    s = ""
    val = 0
    for c in text:
        if (c >= '0') and (c <= '9'):
            s += c
        else:
            break
    if s:
        val = int(s)
    return val

The above works fine, but is there a more pythonic way to do this?

2

There are 2 answers

0
ShadowRanger On BEST ANSWER

This is one of those scenarios where a regex seems reasonable:

 import re

 leadingdigits = re.compile(r'^\d+')

 def read_int_from_string(text):
     return int(leadingdigits.match(text).group(0))

If you hate regex, you can do this to basically push your original loop's logic to the C layer, though it's likely to be slower:

 from itertools import takewhile

 def read_int_from_string(text):
     return int(''.join(takewhile(str.isdigit, text)))
4
Asav Patel On

you can use str.isdigit, how about this one?

>> int(filter(str.isdigit, '128kb/s'))
   128

for Python 3. since filter returns iterable in Python 3

int(''.join(filter(str.isdigit, '128kb/s')))