python怎么去掉字符串的非中文
发布时间:2026-04-22 15:43:46

python去除非中文的方法:
importre
deffind_chinese(file):
pattern=re.compile(r'##39;)
chinese=re.sub(pattern,'',file)
print(chinese)
deffind_unchinese(file):
pattern=re.compile(r'[u4e00-u9fa5];)
unchinese=re.sub(pattern,"",file)
print(unchinese)
if__name__=="__main__":
line="2019年1月3日-python去除空格和换行符的方法1.去除空格strip().strip()#...使用replace('\',''),取代前面的posted@2016-07-1808:53..."
print("原文:")
print(line)
print("保留中文:")
find_chinese(line)
print("保留非中文:")
find_unchinese(line)运行结果:
原文:
2019年1月3日-python去除空格和换行符的方法1.去除空格strip().strip()#...使用replace('',''),取代前面的posted@2016-07-1808:53...
保留中文:
去除空格和换行符的方法一是去除空格,用后面的串替换前面的空格。
保留非中文:
201913-python、strip().strip()#...replace('',''),posted@2016-07-1808:53...有关Python的更多信息,请关注Python视频教程栏目。
