我想在文档中拆分句子,但首先我想在文档中查找小数,版本号等并将点更改为逗号。 例如:
我在Premiere 2.0.1中使用了默认的“ 2.00”质量设置,并且 每十帧指定一个关键帧。
我想将此句子更改为上述形式
我在Premiere 2,0,1和Premiere 2中使用了默认的“ 2,00”质量设置 每十帧指定一个关键帧。
run
此代码找到小数,但我无法将点更改为逗号
答案 0 :(得分:0)
您可以将re.sub
与组引用一起使用。为此,请先将正则表达式中的小数部分包装到(...)
组中,然后在替换字符串中使用\1
和\2
来引用这些组。
>>> s
'I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'
>>> re.sub(r'(\d*)\.(\d+)', r'\1,\2', s)
'I used the default "2,00" quality setting in Premiere 2,0,1, and specified a key frame every ten frames.'
还要注意,通过使.
为可选的\.?
,正则表达式还可以将,
添加到根本没有.
的多位数字中,因此最好删除?
。
但是,恕我直言,“ Premiere 2.0.1”中的.
应该用,
代替。为此,您可以使用负向后查找和超前查找,以确保该数字后没有其他数字或点。
>>> re.sub(r'(?<![\d.])(\d*)\.(\d+)(?![\d.])', r"\1,\2", s)
'I used the default "2,00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'
答案 1 :(得分:0)
您可以使用后续代码示例
s = 'I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.I then ran the 320*240 movie'
s= re.sub(r'(\d*)\.(\d+)', r'\1,\2', s)
print(s)
输出
I used the default "2,00" quality setting in Premiere 2,0,1, and specified a key frame every ten frames.I then ran the 320*240 movie
答案 2 :(得分:0)
简单的解决方案
import re
stringa = 'I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'
Ele = re.search(r'\s*([\d.]+)', stringa)
stringa.replace(Ele.group(),Ele.group().replace(".",",",1))
结果是:
'I used the default "2,00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'