简体   繁体   中英

python regex to replace all windows newlines with spaces

I did this:

from urllib import urlopen
import nltk
url = http://myurl.com
html = urlopen(url).read()
cleanhtml = nltk.clean_html(html)

I now have a long string in python which is full of text interrupted periodically by windows newlines /r/n , and I simply want to remove all of the occurrences of /r/n from the string using a regular expression. First I want to replace it with a space. As such, I did this:

import re
textspaced = re.sub("'\r\n'", r"' '", cleanhtml)

...it didn't work. So what am I doing wrong?

There's no need to use regular expressions, just

htmlspaced = html.replace('\r\n', ' ')

Your program didn't work since you added additional quotes, you only need one set.

Just a small syntax error:

htmlspaced = re.sub(r"\r\n", " ", html)

should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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