Removing the continous newline characters from the pythong string

M

mobil

Hi guys i m trying out newline characters and to clean them up
a\n\n\n\n\n\n\n\n\nsss\n\n\n\n\n\n\n\n\n\n\nvvvv\n\n\n\nvsa\n\n\n\nasf
\n\nafs

hello guys

im trying to replace
\n\n\n\n\n\n\n\n\n with \n

thanks for help

\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n also with \n as the browser gives
\r carrriage returns

thanks for any help or pointers
 
I

Ian Clark

Hi guys i m trying out newline characters and to clean them up
a\n\n\n\n\n\n\n\n\nsss\n\n\n\n\n\n\n\n\n\n\nvvvv\n\n\n\nvsa\n\n\n\nasf
\n\nafs

hello guys

im trying to replace
\n\n\n\n\n\n\n\n\n with \n

thanks for help

\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n also with \n as the browser gives
\r carrriage returns

thanks for any help or pointers

Is this what you were looking for?
import re
message = '\n\r\n\r\n\n\nhello there\n\r\n!\n'
regex = re.compile('[\n\r]+')
regex.sub('\n', s)
'\nhello there\n!\n'

Ian
 
B

Basilisk96

What was I thinking? split() will only work if you have no other
whitespace characters in the string. A regex like "[\n\r]+" is indeed
much more appropriate and robust.

Cheers
-Basilisk96
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top