Skip to main content
 首页 » 编程设计

python之如何仅删除标点符号,如 "."和 ","

2024年11月24日12Terrylee

我想搜索特定的词,例如“收入”或“收入”。为此,我创建了一个词表并在文本中搜索词。

但是,对于带有附加标点符号(如“earnings”)的单词,我的代码不会返回任何结果。或“收入”。现在,我想删除这些标点符号,而不删除数字中的一个点,如“2.4”或任何其他标记,如“%”。

我已经试过了

table = str.maketrans({key: None for key in string.punctuation}) 
text_wo_dots = text.translate(table) 

和

text_wo_dots = re.sub(r'[^\w\s]',' ',text) 

但这删除了所有标点符号。

请您参考如下方法:

我建议,您首先将文本拆分为单独的单词,包括标点符号

text = ["This is an example, it contains 1.0 number and some words."] 
raw_list = text.split() 

现在您可以删除元素末尾的标点符号。

cleaned_words = [] 
for word in raw_list: 
    if word[-1] in ['.', ',', '!', '?']: 
        cleaned_words.append(word[:-1]) 
    else: 
        cleaned_words.append(word) 

注意 1: 如果您的文本包含类似 1. 的数字,对于 1.0,您还需要考虑倒数第二个字符并留下if isdigit() 中的点计算为 True
注意 2:如果有以多个标点符号结尾的句子,您应该运行一个 while 循环来删除它们,然后仅在找不到更多标点符号时才追加。

while True: 
    if word[-1] in ['.', ',', '!', '?']: 
        word = word[:-1] 
    else: 
        break 
 
cleaned_words.append(word)