首页 > 编程语言 > 详细

python去除中文字之间的空格,保留英文单词之间的空格【转】

时间:2020-10-29 19:48:22      阅读:173      评论:0      收藏:0      [点我收藏+]

正则表达式去除中文字之间的空格,保留英文单词之间的空格,对于英文单词中间夹杂着数字的情况,应该保留空格但没有保留

 # -*- coding:utf-8 -*- #
import re

def _clean_space(text): ? match_regex = re.compile(u[\u4e00-\u9fa5。\.,,::《》、\(\)()]{1} +(?<![a-zA-Z])|\d+ +| +\d+|[a-z A-Z]+) ? should_replace_list = match_regex.findall(text) ? order_replace_list = sorted(should_replace_list,key=lambda i:len(i),reverse=True) ? for i in order_replace_list: ? if i == u : ? continue ? new_i = i.strip() ? text = text.replace(i,new_i) ? return text text = "从大约40万 年前开始,一个关键的环境转变 发生了。hello world!" text=text.decode("utf-8") text=_clean_space(text).encode("utf-8") print(text)

 

python去除中文字之间的空格,保留英文单词之间的空格【转】

原文:https://www.cnblogs.com/cydcyd/p/13898633.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!