Eleganz way to get rid of \n

josh logan dear.jay.logan at gmail.com
Mon Sep 1 15:45:06 CEST 2008


On Sep 1, 9:41 am, Wojtek Walczak <gmin... at bzt.bzt> 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.



More information about the Python-list mailing list