国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python使用正則表達(dá)式去除中文文本多余空格,保留英文之間空格

2020-02-15 21:15:28
字體:
供稿:網(wǎng)友

在pdf轉(zhuǎn)為文本的時(shí)候,經(jīng)常會(huì)多出空格,影響數(shù)據(jù)觀感,因此需要去掉文本中多余的空格,而文本中的英文之間的正常空格需要保留,輸入輸出如下:

input:我今天 賺了 10 個(gè)億,老百姓very happy。

output:我今天賺了10個(gè)億,老百姓very happy。

代碼

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

python去除英文單詞之間多余的空格

re.sub(" +", " ", s)

import re s = "     info has been found (+/- 100 pages, and 4.5 mb of .pdf files) now i have to wait untill our team leader has processed it and learns html.     "re.sub(" +", " ", s)

' '.join(s.split())

s = "     info has been found (+/- 100 pages, and 4.5 mb of .pdf files) now i have to wait untill our team leader has processed it and learns html.     "s = ' '.join(s.split())s

更多關(guān)于python使用正則表達(dá)式去除多余空格方法請查看下面的相關(guān)鏈接

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁河县| 雷波县| 富源县| 南昌县| 衡阳市| 顺昌县| 大化| 正安县| 曲阜市| 霍林郭勒市| 庆阳市| 宿州市| 平顶山市| 高安市| 灵寿县| 江口县| 龙南县| 嘉善县| 清涧县| 河南省| 南陵县| 洛川县| 来安县| 正阳县| 忻城县| 柯坪县| 海安县| 府谷县| 文山县| 延边| 塘沽区| 通榆县| 南平市| 将乐县| 宜兴市| 历史| 和顺县| 家居| 察哈| 长丰县| 洪雅县|