Python删除一句话中的文字、符号、标点

简单利用了正则表达式以及Python函数,去掉自己需要测试文本中的数字,文字以及标点符号。这个功能是用在一句中文文本分词处理的基础上,所以直接上一个分词函数,包含了标点等处理的代码:
from string import punctuation 
import re
import jieba
add_punc=',。、【】“”:;()《》‘’{}?!⑦()、%^>℃:.”“^-——=擅长于的&#@¥'
all_punc=punctuation+add_punc
def sentence_cut(x):#cut words and delete punctuation
    x=re.sub(r'[A-Za-z0-9]|/d+','',x)#delet numbers and letters
    testline = jieba.cut(x,cut_all=False)
    testline=' '.join(testline)
    testline=testline.split(' ')
    te2=[]
    for i in testline:
        te2.append(i)
        if i in all_punc:
            te2.remove(i)
    return te2

所以测试一下,效果还是不错滴。注意,在Python中re模块里的punctuation只是包含了英文的特殊标点符号,所以如果要translate.punctuation是需要把这个punctuation·字符串再加上一些你需要的特殊字符等替换一下的。

 x='Python和它你选哪1个,你不要%任性地*操作'

sentence_cut(x)
Out[99]: ['和', '它', '你', '选', '哪个', '你', '不要', '任性', '地', '操作']
在网上看到其他大神的写法,就是如果涉及的文本的处理中,只需要中文,那么就只提取中文这样简单粗暴就好:
text=''.join(re.findall(u'[\u4e00-\u9fff]+', text))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章