繁体   English   中英

python 正则表达式用空格替换所有 windows 换行符

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM