Re: Eleganz way to get rid of \n

2008-09-01 Thread Fredrik Lundh

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




--
http://mail.python.org/mailman/listinfo/python-list


Re: Eleganz way to get rid of \n

2008-09-01 Thread Bruno Desthuilliers

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)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Eleganz way to get rid of \n

2008-09-01 Thread josh logan
On Sep 1, 9:41 am, Wojtek Walczak <[EMAIL PROTECTED]> wrote:
> 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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Eleganz way to get rid of \n

2008-09-01 Thread Wojtek Walczak
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/
--
http://mail.python.org/mailman/listinfo/python-list

Re: Eleganz way to get rid of \n

2008-09-01 Thread josh logan
On Sep 1, 9:25 am, Hans Müller <[EMAIL PROTECTED]> wrote:
> 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')
--
http://mail.python.org/mailman/listinfo/python-list