您好,登錄后才能下訂單哦!
在pdf轉為文本的時候,經常會多出空格,影響數據觀感,因此需要去掉文本中多余的空格,而文本中的英文之間的正常空格需要保留,輸入輸出如下:
input:我今天 賺了 10 個億,老百姓very happy。
output:我今天賺了10個億,老百姓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
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
更多關于python使用正則表達式去除多余空格方法請查看下面的相關鏈接
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。