Python: Strip Everything but Numbers

11,606

Solution 1

Instead of strip, select for the numbers with a regular expression:

import re

numbers = re.compile(r'\d+(?:\.\d+)?')
numbers.findall("It took 2.3 seconds")

Demo:

>>> import re
>>> numbers = re.compile(r'\d+(?:\.\d+)?')
>>> numbers.findall("It took 2.3 seconds")
['2.3']

This returns a list of all matches; this lets you find multiple numbers in a string too:

>>> numbers.findall("It took between 2.3 and 42.31 seconds")
['2.3', '42.31']

Solution 2

If all you want to do is remove all characters that aren't in another string, I'd suggest something like the following:

>>> to_filter = "It took 2.3 seconds"
>>> "".join(_ for _ in to_filter if _ in ".1234567890")
'2.3'

It's an extremely naive way to extract numbers, however. You should use the answer by Martijn Pieters if you want more than just a simple character filter like you asked for.

Share:
11,606
aldorado
Author by

aldorado

Updated on June 25, 2022

Comments

  • aldorado
    aldorado almost 2 years

    I have to extract a number (a measured time value) from each of several strings. How could I do this elegantly? All numbers are positive and have a maximum of two decimal places. (E.g.: 2.3/ 40.09/ 101.4 - no numbers in E notation). The code I am looking for should do something like the following pseudocode:

    >>> "It took 2.3 seconds".strip(everything but ".1234567890")
    2.3
    
  • Peter DeGlopper
    Peter DeGlopper over 10 years
    There are some complicated ones here that deal with negative numbers and scientific notation - those may be out of scope for the OP. stackoverflow.com/questions/2293780/…
  • Martijn Pieters
    Martijn Pieters over 10 years
    @PeterDeGlopper: Since the OP specifically wanted to strip everything but .0123456789 I kept it simple.