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?
This is one of those scenarios where a regex seems reasonable:
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: