Removing newlines from string on windows (without replacing)

T

Tom

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"
 
U

Ulrich Eckhardt

Tom said:
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
 
T

Tom

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
 
J

John Yeung

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
 
A

Aahz

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 ([email protected]) <*> 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
 
T

Tom

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top