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!]

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