Python 替换字符串中的分数
Python Replace fractions in a string
我正在尝试替换字符串中不同格式的分数。有些格式看起来像 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'
我尝试过的:
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
我也试过 from fractions import Fraction
和 from __future__ import division
但无法让它们在字符串上工作。
这是一个解决方案,使用 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
如果你在问题中提到的分数部分在语法上是错误的,即实际数据与你在问题中显示的不同。
您可以将 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']
我正在尝试替换字符串中不同格式的分数。有些格式看起来像 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'
我尝试过的:
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
我也试过 from fractions import Fraction
和 from __future__ import division
但无法让它们在字符串上工作。
这是一个解决方案,使用 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
如果你在问题中提到的分数部分在语法上是错误的,即实际数据与你在问题中显示的不同。
您可以将 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']