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

Python去除文本所有标点符号

时间:2019-02-11 22:24:18

相关推荐

Python去除文本所有标点符号

去除标点符号方式多种多样,这里介绍两种自己常用的。

1、python自带punctuation包,可以消除所有中文标点符号。

import re,stringfrom zhon.hanzi import punctuationtext = " Hello, world! 这,是:我;第!一个程序\?()()<>《》 "print(re.sub(r"[%s]+" %punctuation, "",text))

Hello world 这是我第一个程序

2、自己定义标点符号集,即可以消除中文标点符号也可以消除英文标点符号。

import re,stringtext = " Hello, world! 这,是:我;第!一个程序\?()()<>《》 "punc = '~`!#$%^&*()_+-=|\';":/.,?><~·!@#¥%……&*()——+-=“:’;、。,?》《{}'print(re.sub(r"[%s]+" %punc, "",text))

Hello world 这是我第一个程序

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