1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 去除英文或中文文本中标点和数字及指定字符串

python 去除英文或中文文本中标点和数字及指定字符串

时间:2018-08-27 04:04:06

相关推荐

python 去除英文或中文文本中标点和数字及指定字符串

例一

from string import punctuationfrom string import digitsimport redef preprocess_English(text,rm_list):text = re.sub(r'[{}]+'.format(punctuation+digits), '', text)for rm_item in rm_list:text = text.replace(rm_item, '')return textrm_list = ['pg','\n','\t'] #string you want to remove from text,'\n' and '\t' must be includetext_file='LifeofEdwinForrest.txt'with open(text_file,'r',encoding='utf-8') as f:text = f.read()text = text.lower()print(preprocess_English(text,rm_list))

例二

def preprocess_Chinese(text):from zhon.hanzi import punctuation text = re.sub(r'[{}]+'.format(punctuation),'',text)return textdef preprocess_English(text):from string import punctuationtext = re.sub(r'[{}]+'.format(punctuation),'',text)return text

参考:

[1] NLP:最全去掉文本中的中英文标点符号大法 [CSDN]

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