f



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
0
heintest (27)
9/1/2008 1:25:03 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

5 Replies
254 Views

Similar Articles

[PageSpeed] 32

On Sep 1, 9:25=A0am, Hans M=FCller <HeinT...@web.de> wrote:
> Hello,
>
> I'm quite often using this construct:
>
> for l in open("file", "r"):
> =A0 =A0 =A0 =A0 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"):
> =A0 =A0 =A0 =A0 while l[-1] in "\r\n":
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 l =3D 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 =3D open(fname)
for x in f:
    line =3D x.rstrip('\r\n')
0
9/1/2008 1:37:14 PM
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/
0
gminick1 (69)
9/1/2008 1:41:25 PM
On Sep 1, 9:41=A0am, Wojtek Walczak <gmin...@bzt.bzt> wrote:
> On Mon, 01 Sep 2008 15:25:03 +0200, Hans M=FCller wrote:
> > I'm quite often using this construct:
>
> > for l in open("file", "r"):
> > =A0 =A0do something
> > Has someone a better solution ?
>
> The most general would be to use rstrip() without
> arguments:
>
>
>
> >>> a=3D"some string\r\n"
> >>> a.rstrip()
> 'some string'
>
> but be careful, because it will also cut whitespaces:
>
>
>
> >>> a=3D"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.
0
9/1/2008 1:45:06 PM
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)
0
9/1/2008 2:40:42 PM
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>

0
fredrik2101 (5275)
9/1/2008 2:45:24 PM
Reply: