473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Eleganz way to get rid of \n

Hello,

I'm quite often using this construct:

for l in open("file", "r"):
do something
here, l contains the \n or \r\n on windows at the end.
I get rid of it this way:

for l in open("file", "r"):
while l[-1] in "\r\n":
l = l[:-1]

I find this a little bit clumsy, but it works fine.

Has someone a better solution ?

Thanks

Hans
Sep 1 '08 #1
5 1731
On Sep 1, 9:25*am, Hans Müller <HeinT...@web.dewrote:
Hello,

I'm quite often using this construct:

for l in open("file", "r"):
* * * * do something

here, l contains the \n or \r\n on windows at the end.
I get rid of it this way:

for l in open("file", "r"):
* * * * while l[-1] in "\r\n":
* * * * * * * * l = l[:-1]

I find this a little bit clumsy, but it works fine.

Has someone a better solution ?

Thanks

Hans
Can you do this:

f = open(fname)
for x in f:
line = x.rstrip('\r\n')
Sep 1 '08 #2
On Mon, 01 Sep 2008 15:25:03 +0200, Hans Müller wrote:
I'm quite often using this construct:

for l in open("file", "r"):
do something
Has someone a better solution ?
The most general would be to use rstrip() without
arguments:
>>a="some string\r\n"
a.rstrip()
'some string'
>>>
but be careful, because it will also cut whitespaces:
>>a="some string\t \r\n"
a.rstrip()
'some string'
>>>
so maybe you could do this:
>>a.rstrip('\n').rstrip('\r')
'some string\t '
>>>
HTH.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
Sep 1 '08 #3
On Sep 1, 9:41*am, Wojtek Walczak <gmin...@bzt.bztwrote:
On Mon, 01 Sep 2008 15:25:03 +0200, Hans Müller wrote:
I'm quite often using this construct:
for l in open("file", "r"):
* *do something
Has someone a better solution ?

The most general would be to use rstrip() without
arguments:
>a="some string\r\n"
a.rstrip()
'some string'

but be careful, because it will also cut whitespaces:
>a="some string\t \r\n"
a.rstrip()
'some string'

so maybe you could do this:
>a.rstrip('\n').rstrip('\r')
'some string\t '

HTH.

--
Regards,
Wojtek Walczak,http://tosh.pl/gminick/
You can send both '\n' and '\r' in one rstrip call. No need for 2
separate calls.
Sep 1 '08 #4
Hans Müller a écrit :
Hello,

I'm quite often using this construct:

for l in open("file", "r"):
do something

here, l contains the \n or \r\n on windows at the end.
I get rid of it this way:

for l in open("file", "r"):
while l[-1] in "\r\n":
l = l[:-1]

I find this a little bit clumsy,
indeed.
but it works fine.

Has someone a better solution ?
help(str.rstrip)
Sep 1 '08 #5
Hans Müller wrote:
I'm quite often using this construct:

for l in open("file", "r"):
do something

here, l contains the \n or \r\n on windows at the end.
nope -- if you open a file in text mode (without the "b"), the I/O layer
will translate "\r\n" to "\n" on Windows.

if you want even more robust behaviour, use the "U" flag (for universal
newlines); that'll handle old-style Mac files too.

(as others have pointed out, a plain rstrip() is usually the best choice
anyway. giving meaning to trailing whitespace in text files is usually
a really lousy idea).

</F>

Sep 1 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.