Eleganz way to get rid of \n

Wojtek Walczak gminick at bzt.bzt
Mon Sep 1 15:41:25 CEST 2008


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/



More information about the Python-list mailing list