python 去除所有的中文 英文标点符号

去除英文标点符号

python的string模块下的 punctuation 包含所有的英文标点符号,所以用replace()一下就可以去除。

代码示例:

import string
stri = 'today is friday, so happy..!!!'
punctuation_string = string.punctuation

print("所有的英文标点符号:", punctuation_string)
for i in punctuation_string:
    stri = stri.replace(i, '')
print(stri)

结果:

所有的英文标点符号: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
today is friday so happy

注意:

string.punctuation中的标点符号只有英文

去除中文标点符号:

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

代码示例:

from zhon.hanzi import punctuation

str = '今天周五,下班了,好开心呀!!'
punctuation_str = punctuation
print("中文标点符合:", punctuation_str)
for i in punctuation:
    str = str.replace(i, '')
print(str)

结果:

中文标点符合: "#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、 、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·!?。。
今天周五下班了好开心呀

参考博客:python之去除文本标点符号

猜你喜欢

转载自blog.csdn.net/weixin_38819889/article/details/105389248