Removing the continous newline characters from the pythong string

Ian Clark turbana at gmail.com
Tue May 1 17:46:18 EDT 2007


On 1 May 2007 14:30:12 -0700, mobil <mobiledreamers at gmail.com> wrote:
> 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



More information about the Python-list mailing list