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. Advertisements

  2. 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. ;)

    Ulrich Eckhardt, Jul 26, 2009
    1. Advertisements

  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 == "nt":
    s = sauce.rstrip("\r\n")
    s = sauce.replace("\n", "")

    This works fine now, ( returns nt on windows and posix on
    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
    Tom, Jul 26, 2009
  4. Tom

    John Yeung Guest

    Well, this is doing two different things. When 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 Yeung, Jul 26, 2009
  5. Tom

    Aahz Guest

    Use open(fname, 'U')
    Aahz () <*>

    "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
  6. Tom

    Tom Guest

    (Even if it so happens sauce
    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.
    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.