The Split String Function - How to preserve whitespace?

Just just at xs4all.nl
Sat Jul 8 16:03:29 EDT 2006


In article <1152388538.357689.143690 at 35g2000cwc.googlegroups.com>,
 "daftspaniel at gmail.com" <daftspaniel at gmail.com> wrote:

> I am trying to scan the lines in the string text:
>         lines = text.split('\n')
> 
> seemed to do the job but has the side effect of stripping the
> whitespace. As I am reading Python source in this text and checking
> identation this is a bit annoying :-)
> 
> How can I stop this happening? Doesn't look like an option from the
> documents. Do I have to use a regexp (scary unchartered stuff for
> me...)?

   >>> "a\nb\nc\n".splitlines(True)
   ['a\n', 'b\n', 'c\n']

Just



More information about the Python-list mailing list