extract digits in a simple way from a python string

Question:

I have a string that stores a number and a unit for example

x= '$120'
y = ' 90 Degrees F'
banana = '200 kgm'
orange = '300 gm'
total_weight = banana + orange/1000 

and for example I want to add the weights

total_weight  = 200 + 300/1000

Thanks!

I’m trying to extract the numbers only to do some operations with these… any idea of what the simplest way to do this? I’m only dealing with these two formats i.e. digits are at the begining or at the end of the string…

Asked By: Ka Ra

||

Answers:

The simplest way to extract a number from a string is to use regular expressions and findall.

>>> import re
>>> s = '300 gm'
>>> re.findall('d+', s)
['300']
>>> s = '300 gm 200 kgm some more stuff a number: 439843'
>>> re.findall('d+', s)
['300', '200', '439843']

It might be that you need something more complex, but this is a good first step.

Note that you’ll still have to call int on the result to get a proper numeric type (rather than another string):

>>> map(int, re.findall('d+', s))
[300, 200, 439843]
Answered By: senderle
>>> x='$120'
>>> import string
>>> a=string.maketrans('','')
>>> ch=a.translate(a, string.digits)
>>> int(x.translate(a, ch))
120
Answered By: shiva

This regular expression handles floats as well

import re
re_float = re.compile(r'd*.?d+')

You could also add a group to the expression that catches your weight units.

re_banana = re.compile(r'(?P<number>d*.?d+)s?(?P<uni>[a-zA-Z]+)')

You can access the named groups like this re_banana.match("200 kgm").group('number').

I think this should help you getting started.

Answered By: Francis

If you’re doing some sort of math with the numbers you might also want to know the units. Given your input restrictions (that the input string contains unit and value only), this should correctly return both (you’ll just need to figure out how to convert units into common units for your math).

def unit_value(str):
    m = re.match(r'([^d]*)(d*.?d+)([^d]*)', str)
    if m:
        g = m.groups()
        return ' '.join((g[0], g[2])).strip(), float(g[1])
    else:
        return int(str)
Answered By: John Gaines Jr.

Without using regex, you can just do:

def get_num(x):
    return int(''.join(ele for ele in x if ele.isdigit()))

Result:

>>> get_num(x)
120
>>> get_num(y)
90
>>> get_num(banana)
200
>>> get_num(orange)
300

EDIT :

Answering the follow up question.

If we know that the only period in a given string is the decimal point, extracting a float is quite easy:

def get_num(x):
    return float(''.join(ele for ele in x if ele.isdigit() or ele == '.'))

Result:

>>> get_num('dfgd 45.678fjfjf')
45.678
Answered By: Akavall
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.