Python替换字符串中的分数



我试图在不同格式的字符串中替换分数。有些格式看起来像1/2,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'

我已经试过了:

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

我也尝试过from fractions import Fractionfrom __future__ import division,但不能让这些在字符串上工作。

您可以将fractions包与re.sub包一起使用。

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] 

给你:

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

这是一个使用Series.str.replace和捕获组的解决方案,然后使用lambda将小数值转换为浮点数,然后进行替换。

df['col'].str.replace('(s(?:d+)?.?d+/d+)', lambda x: ' '+str(eval(x.group(0).replace(' ','+').replace('-',  '+'))), regex=True)
0       This is the first fraction 0.5
1    This is the second fraction 3.125
2     This is the third fraction 20.25
Name: col, dtype: object

PS:因为,它使用eval将字符串小数值转换为浮点值,如果你在问题中提到的小数部分语法错误,即实际数据与你在问题中显示的数据不同,它将抛出syntaxError