Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 393
aldorado Avatar asked Oct 25 '13 15:10

aldorado


2 Answers

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']
like image 116
Martijn Pieters Avatar answered Sep 19 '22 16:09

Martijn Pieters


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.

like image 32
Thane Brimhall Avatar answered Sep 19 '22 16:09

Thane Brimhall