The Split String Function - How to preserve whitespace?

Discussion in 'Python' started by daftspaniel@gmail.com, Jul 8, 2006.

  1. Guest

    Hi Folks,

    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...)?

    Thanks,
    Davy Mitchell

    The Good Ol' Blog
    http://www.latedecember.com/sites/personal/davy/
    , Jul 8, 2006
    #1
    1. Advertising

  2. Just Guest

    In article <>,
    "" <> 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
    Just, Jul 8, 2006
    #2
    1. Advertising

  3. Guest

    , Jul 8, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    11
    Views:
    1,094
    Mike Moore [MSFT]
    Nov 14, 2003
  2. Replies:
    1
    Views:
    560
    Joe Kesselman
    May 19, 2006
  3. Mike

    Preserve whitespace

    Mike, Sep 29, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    193
    Guest
    Apr 21, 2005
  4. Grzegorz Chrupala
    Replies:
    2
    Views:
    193
    Grzegorz Chrupala
    Jun 30, 2003
  5. Sam Kong
    Replies:
    5
    Views:
    227
    Rick DeNatale
    Aug 12, 2006
Loading...

Share This Page