1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 去除所有的中文 英文标点符号

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

时间:2020-07-18 02:58:41

相关推荐

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

去除英文标点符号

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

代码示例:

import stringstri = 'today is friday, so happy..!!!'punctuation_string = string.punctuationprint("所有的英文标点符号:", 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 punctuationstr = '今天周五,下班了,好开心呀!!'punctuation_str = punctuationprint("中文标点符合:", punctuation_str)for i in punctuation:str = str.replace(i, '')print(str)

结果:

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。