You can use a regular expression (using the re
module) to accomplish the same thing. The example below matches runs of [^\d.]
(any character that’s not a decimal digit or a period) and replaces them with the empty string. Note that if the pattern is compiled with the UNICODE
flag the resulting string could still include non-ASCII numbers. Also, the result after removing “non-numeric” characters is not necessarily a valid number.
>>> import re
>>> non_decimal = re.compile(r'[^\d.]+')
>>> non_decimal.sub('', '12.34fe4e')
'12.344'