Q:

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

Using Python, how can/should I parse a string that has a number, followed by other characters, to an int? The specific problem I am trying to solve is parsing the first number out of a string containing a number followed by an arbitrary amount of other characters, including, possibly other numbers, which I am not interested in.  
For example, if the string is "12//1"  I need to get just the 12 to an integer.

A:

If you want to extract the digits in the string:
int(''.join(c for c in s if c.isdigit()))

A:

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 strops at the first nondigit character.
\d means single digit, \d+ means match at least one digit and (\d+) together means put what have you found in a group 1.

A:

Looks like a job for itertools.takewhile:
>>> from itertools import takewhile
>>> s = '12//'
>>> int(''.join(takewhile(str.isdigit, s)))
12

