文本过滤(python正则)
程序员文章站
2022-07-12 18:59:14
...
1、保留中文(将非中文替换为" ")
def filterCharacter(s):
import re
r1 = re.sub(u"[^\u4e00-\u9fa5]", " ", s)
return r1
2、连续空格替换为单空格
def filterCharacter(s):
import re
r1 = re.sub(r"\s{1,}", " ", r1)
return r1
3、去掉标点数字等信息
def filterCharacter(s):
import re
r1 = re.sub("[A-Za-z0-9\[\`\~\!\@\#\$\^\&\*\(\)\=\|\{\}\'\:\;\'\,\[\]\.\<\>\/\?\~\!\@\#\\\&\*\%]", "", s)
return r1
<<<未完待续