Parse string to int when string contains a number + extra characters

If you want to extract the digits in the string:

int(''.join(c for c in s if c.isdigit()))

Looks like a job for itertools.takewhile:

>>> from itertools import takewhile
>>> s = '12//'
>>> int(''.join(takewhile(str.isdigit, s)))
12

I would use this regular expression:

import re
try:
    print int(re.compile("(\d+)").match('12//').group(1))
except:
    print "there was no number"

It will extract all digits and stops at the first non-digit character.

\d means single digit, \d+ means match at least one digit and (\d+) together means return what have you found in group 1.


This is kind of a cool technique, but it may be overkill for just this if it's only the format you describe:

import string
potential_bad_characters = string.ascii_puctuation + string.ascii_letters

int(my_string.translate(None,potential_bad_characters ))

#Or
int(mystring.rstrip(potential_bad_characters))

#Or
int(filter(str.isdigit,my_string))

#Or (kudos @JonClements)
potential_bad_characters = ''.join(map(chr, range(256))).replace('0123456789', '')
...

Tags:

Python