Re: Any algorithm to preserve whitespaces?

Discussion in 'Python' started by Dave Angel, Jan 23, 2013.

  1. Dave Angel

    Dave Angel Guest

    On 01/23/2013 04:20 AM, Santosh Kumar wrote:
    > I am in a problem.
    >
    > words = line.split(' ')
    >
    > preserve whitespaces but the problem is it writes an additional line
    > after every line.
    >


    Think about what you said. It might be clearer if you wrote:

    "but the problem is it doesn't strip off the newline (which is whitespace)."

    You might want to fix it by doing an rstrip(), as Peter said, or you
    might want to check if the last character is \n, and delete it if so.

    Or you might want to fix the other logic where you use the reconstituted
    line, making sure it doesn't add an extra newline to a line that already
    has one.

    Best answer depends on whether there might be other whitespace at the
    end of the line, and on whether you consider the newline part of the
    last field on the line.

    Chances are that Peter's response is the one you want, but I had to
    point out that without a spec, we're really just guessing. For another
    example, suppose that some of the words in the file are separated by
    tabs. If so, perhaps you'd better rethink the whole split logic.




    --
    DaveA
     
    Dave Angel, Jan 23, 2013
    #1
    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. Santosh Kumar

    Any algorithm to preserve whitespaces?

    Santosh Kumar, Jan 19, 2013, in forum: Python
    Replies:
    0
    Views:
    147
    Santosh Kumar
    Jan 19, 2013
  2. Lie Ryan
    Replies:
    0
    Views:
    148
    Lie Ryan
    Jan 19, 2013
  3. Mitya Sirenef

    Re: Any algorithm to preserve whitespaces?

    Mitya Sirenef, Jan 19, 2013, in forum: Python
    Replies:
    0
    Views:
    146
    Mitya Sirenef
    Jan 19, 2013
  4. Santosh Kumar

    Re: Any algorithm to preserve whitespaces?

    Santosh Kumar, Jan 23, 2013, in forum: Python
    Replies:
    0
    Views:
    110
    Santosh Kumar
    Jan 23, 2013
  5. Peter Otten
    Replies:
    0
    Views:
    110
    Peter Otten
    Jan 23, 2013
Loading...

Share This Page