Skip to main content
 首页 » 编程设计

python之如何在处理前从字符串中去除额外的句号 (.) 或清理数据

2025年05月04日28sxdcgaq8080

示例:

string_1 = 'Hello,How are you...?' 
string_2 = 'I am having problem....using this device..' 

我想删除多余的 '.'。我想要像 find&replace 这样的东西。我想为此包括 RegEx,但并不擅长。

我尝试了以下方法:

import re      
v = re.sub('.', '.', string_1) 

但这并没有给我我想要的。如何操作re.sub()中的第一个'.'?

请您参考如下方法:

您可以在正则表达式中使用 + 并否定 . 因为它匹配正则表达式中的所有内容

代码:

re.sub("\.+", ".", string_2) 

输出:

'I am having problem.using this device.'