Search code examples
pythonregexdecimalfractions

Python Replace fractions in a string


I am trying to replace fractions in a string that come in different formats. Some of the formats look like 1/2 , 1 1/2, 1-1/2.

Input='This is the first fraction 1/2'
Input_two='This is the second fraction 3-1/8'
Input_three='This is the third fraction 20 1/4'

Output='This is the first fraction 0.5'
Output_two='This is the first fraction 3.12'
Output_three='This is the first fraction 20.25'

What I have tried:

df['col]=df['col'].apply(lambda x: re.sub('\d\d?\d?.?\d+/\d+','1.5',str(x))
But this only works if you put the value in each time and a thousand different fractions

I have also tried from fractions import Fraction and from __future__ import division but cannot get these to work on strings.


Solution

  • You can use the fractions package together with a re.sub to do this.

    import re
    from fractions import Fraction
    
    Inputs=[
        'This is the first fraction 1/2',
        'This is the second fraction 3-1/8',
        'This is the third fraction 20 1/4',
        'This is the first fraction 1/2 second fraction 3-1/8 third: 10 3/4'
    ]
    
    def frac2string(s):
        i, f = s.groups(0)
        f = Fraction(f)
        return str(int(i) + float(f))
    
    [re.sub(r'(?:(\d+)[-\s])?(\d+/\d+)', frac2string, s) for s in Inputs] 
    

    which gives you:

    ['This is the first fraction 0.5',
     'This is the second fraction 3.125',
     'This is the third fraction 20.25',
     'This is the first fraction 0.5 second fraction 3.125 third: 10.75']