[英]python regex to replace all windows newlines with spaces
我這樣做了:
from urllib import urlopen
import nltk
url = http://myurl.com
html = urlopen(url).read()
cleanhtml = nltk.clean_html(html)
我現在在 python 中有一個長字符串,其中充滿了由 windows 換行符/r/n
定期中斷的文本,我只想使用正則表達式從字符串中刪除所有出現的 /r/n 。 首先,我想用空格替換它。 因此,我這樣做了:
import re
textspaced = re.sub("'\r\n'", r"' '", cleanhtml)
......它沒有工作。 那么我做錯了什么?
不需要使用正則表達式,只需
htmlspaced = html.replace('\r\n', ' ')
由於您添加了額外的引號,您的程序無法正常工作,您只需要一組。
只是一個小的語法錯誤:
htmlspaced = re.sub(r"\r\n", " ", html)
應該管用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.