文本预处理去除标点符号
#!/usr/bin/env python # coding=utf-8 from string import punctuation import re import sys reload(sys) sys.setdefaultencoding('utf-8') import codecs # 英文标点符号+中文标点符号 punc = punctuation + u'.,;《》?!“”‘’@#¥%…&×()——+【】{};;●,。&~、|\s::' print punc fr = codecs.open('./train_jkm.txt',encoding='utf-8') fw = codecs.open('./train_clean.txt','w',encoding='utf-8') # 利用正则表达式替换为一个空格 for line in fr: line = re.sub(r"[{}]+".format(punc)," ",line) fw.write(line+' ') fr.close() fw.close()