Removing newlines from string on windows (without replacing)

Discussion in 'Python' started by Tom, Jul 25, 2009.

  1. Tom

    Tom Guest

    This is my first post to this mailing list, so hi :)

    I have an annoying problem. While I mainly use Linux when I distribute
    this program to friends and on the internet, it'll get used on Windows.
    So, I tested my python program on my Windows Vista dual boot, running
    the same version of python (2.6) as my Linux, and got an error at the
    following code.

    s = sauce.replace("\n", "")

    Sauce is a string, read from a file, that I need to remove newlines
    from. This code works fine in Linux, but not in Windows. rstrip("\n")
    won't work for me, so anybody know how to get this working on Windows?

    Thanks! :D

    --print "Tom"
    Tom, Jul 25, 2009
    #1
    1. Advertising

  2. Tom wrote:
    > s = sauce.replace("\n", "")
    >
    > Sauce is a string, read from a file, that I need to remove newlines
    > from. This code works fine in Linux, but not in Windows. rstrip("\n")
    > won't work for me, so anybody know how to get this working on Windows?


    I'm pretty sure this works regardless of the system. What might change is
    the way that a line is terminated: MS Windows by default uses a CR/LF
    ('\r\n') pair, while most other systems just an LF ('\n').

    I'd adapt the code to accept either line ending on either system.

    If that's not what you want, you should improve your "doesn't work"
    description. ;)

    Uli
    Ulrich Eckhardt, Jul 26, 2009
    #2
    1. Advertising

  3. Tom

    Tom Guest

    Sorry for not providing enough information - and Rhodri, for sending
    my second message I used the GMX webmail client rather than my usual
    Linux client as I was testing it on windows. That must have screwed it
    up, I need to download Thunderbird on Mozzila.

    The thing that was messing it up was that the endlines are handled
    differently on each each OS, so I changed the code to strip the
    endlines to be:

    if os.name == "nt":
    s = sauce.rstrip("\r\n")
    else:
    s = sauce.replace("\n", "")

    This works fine now, (os.name returns nt on windows and posix on
    linux)
    Thanks for the help, and sorry for slow response and lack of detail,
    I'm still trying to get used to this usenet thing :p

    --
    print "Tom"
    Tom, Jul 26, 2009
    #3
  4. Tom

    John Yeung Guest

    On Jul 26, 1:13 pm, Tom <> wrote:
    > The thing that was messing it up was that the endlines are handled
    > differently on each each OS, so I changed the code to strip the
    > endlines to be:
    >
    >     if os.name == "nt":
    >         s = sauce.rstrip("\r\n")
    >     else:
    >         s = sauce.replace("\n", "")


    Well, this is doing two different things. When os.name is 'nt', you
    are getting rid of all *trailing* CRLFs. Otherwise, you are getting
    rid of all LFs *anywhere in the string*. (Even if it so happens sauce
    will only ever have LFs at the end, it's still better to use the
    method that is closest to your intended meaning.)

    Personally, I prefer using sauce.rstrip() with no parameters (one of
    the suggestions Rhodri mentioned) if it's not important to preserve
    trailing spaces, since this will work regardless of end-of-line style
    (and typically, trailing spaces, tabs, etc. are undesirable anyway).

    John
    John Yeung, Jul 26, 2009
    #4
  5. Tom

    Aahz Guest

    In article <>,
    Tom <> wrote:
    >
    >I have an annoying problem. While I mainly use Linux when I distribute
    >this program to friends and on the internet, it'll get used on Windows.
    >So, I tested my python program on my Windows Vista dual boot, running
    >the same version of python (2.6) as my Linux, and got an error at the
    >following code.
    >
    >s = sauce.replace("\n", "")
    >
    >Sauce is a string, read from a file, that I need to remove newlines
    >from. This code works fine in Linux, but not in Windows. rstrip("\n")
    >won't work for me, so anybody know how to get this working on Windows?


    Use open(fname, 'U')
    --
    Aahz () <*> http://www.pythoncraft.com/

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. :)"
    --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22
    Aahz, Jul 26, 2009
    #5
  6. Tom

    Tom Guest

    > (Even if it so happens sauce
    > will only ever have LFs at the end, it's still better to use the
    > method that is closest to your intended meaning.)


    Oh, you're right. I'll change that now. I suppose removing all
    endlines will be better, incase, somehow, endlines in the middle of
    the line arrises.

    > if it's not important to preserve trailing spaces


    The way I parse the file currently splits away all whitespace later
    on. While I could change this, I have a nice system worked out and my
    code is working, so it's not top priority.
    Tom, Jul 26, 2009
    #6
    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. Grant Edwards
    Replies:
    4
    Views:
    415
    Grant Edwards
    Sep 29, 2005
  2. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    255
    Chris Hohmann
    Apr 11, 2005
  3. John

    Removing trailing newlines -

    John, Apr 23, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    159
    J. Gleixner
    Apr 24, 2008
  4. Jensen

    Newbie: Replacing double newlines

    Jensen, Aug 8, 2008, in forum: Perl Misc
    Replies:
    9
    Views:
    101
    Tad J McClellan
    Aug 12, 2008
  5. John Black

    Why is this sub removing newlines??

    John Black, Dec 5, 2013, in forum: Perl Misc
    Replies:
    29
    Views:
    215
    Charles DeRykus
    Dec 13, 2013
Loading...

Share This Page