python之去除文本标点符号

        今天做一个文本相似度的小任务,利用python的“Levenshtein”包可对比两个文本的相似度。为了消除标点符号的影响,需要去除标点,python的string模块下的punctuation包含所有的英文标点符号。所以用replace()一下就可以去除:

Example 1:
import string
s = 'today is friday, so happy..!!!'
for c in string.punctuation:
    s = s.replace(c,'')
print(s)
Result:
today is friday so happy

        string.punctuation中的标点符号只有英文,如果是中文文本,可以调用zhon包的zhon.hanzi.punctuation函数即可得到中文的标点符号集合。

Example 2:
from zhon.hanzi import punctuation
a = '今天周五,下班了,好开心呀!!'
for i in punctuation:
    a = a.replace(i,'')
print(a)
Result:
今天周五下班了好开心呀

<( ̄︶ ̄)↗[GO!]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章