aldorado
aldorado

Reputation: 4844

Python: Strip Everything but Numbers

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

Upvotes: 5

Views: 14229

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1121486

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']

Upvotes: 12

Thane Brimhall
Thane Brimhall

Reputation: 9555

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.

Upvotes: 9

Related Questions