http://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string
import re
re.sub("/D", "", "aas30dsa20")
'3020'
/D
matches any non-digit character so, the code above, is essentially replacing every non-digit character for the empty string.
Or you can use filter
, like so (in Python 2k):
filter(lambda x: x.isdigit(), "aas30dsa20")
Since in Python 3k, filter
returns an iterator instead of a list
, you can use the following instead:
>>> ''.join(filter(lambda x: x.isdigit(), "aas30dsa20"))
'3020'
s=''.join(i for i in s if i.isdigit())
Another generator variant.
>>> s = "foo200bar"
>>> new_s = "".join(i for i in s if i in "0123456789")
>>> text = "9jk78k.9k87h.ji09j9oj"
>>> print "".join(i for i in text if i in ".0123456789").replace(".",",",1).replace(".","").replace(",",".")
978.987099