Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Removing newlines from string on windows (without replacing)

Reply
Thread Tools

Removing newlines from string on windows (without replacing)

 
 
Tom
Guest
Posts: n/a
 
      07-25-2009
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!

--print "Tom"

 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      07-26-2009
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


 
Reply With Quote
 
 
 
 
Tom
Guest
Posts: n/a
 
      07-26-2009
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

--
print "Tom"
 
Reply With Quote
 
John Yeung
Guest
Posts: n/a
 
      07-26-2009
On Jul 26, 1:13*pm, Tom <(E-Mail Removed)> 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
 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      07-26-2009
In article <(E-Mail Removed)>,
Tom <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> 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
 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      07-26-2009
> (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Disabling" raw string to print newlines kuratkull@kuratkull.com Python 5 05-19-2008 02:29 PM
Removing trailing newlines - John Perl Misc 7 04-24-2008 04:44 PM
string length and newlines Rob Javascript 16 01-16-2008 11:54 PM
xml.sax removing newlines from attribute value? Grant Edwards Python 4 09-29-2005 08:11 PM
Loading a hash from a string containing newlines Scott Bass Perl Misc 3 05-11-2005 08:22 PM



Advertisments