regular expresson for Unix and Dos Lineendings wanted

Tim Williams (gmail) tdwdotnet at gmail.com
Fri Feb 24 09:37:40 EST 2006


On 24/02/06, John Zenger <john_zenger at yahoo.com> wrote:
>
> Franz Steinhaeusler wrote:
> >
> > Thank you all for the replies.
> > But I still don't have a solution.
> >
> re.sub(r"\s+[\n\r]+", lambda x: x.expand("\g<0>"). \
>      lstrip(" \t\f\v"),text).rstrip()
>
>
> But I think your code is a lot easier to read.  :)
>

How about:

>>> linesep = '\n'
>>> text = 'erewr  \t  \r\nafjdskl  \r\n  \r\npythonlist'
>>> linesep.join([ x.rstrip() for x in
text.replace('\r\n','\n').replace('\r','\n').split('\n')
])
'erewr\nafjdskl\n\npythonlist'

Which right-strips the line endings, tabs and spaces from the ends of lines
in a string derived from either Windows, Mac and Unix  or mixed files

HTH :)



--

Tim Williams
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060224/21df7c6c/attachment.html>


More information about the Python-list mailing list