How to get rid the new line

Bengt Richter bokr at oz.net
Mon Jul 1 17:16:44 EDT 2002


On Sun, 30 Jun 2002 15:04:52 -0600, "Bjorn Pettersen" <BPettersen at NAREX.com> wrote:

>> From: Frank Tobin [mailto:ftobin at neverending.org]=20
>>=20
>> Fredrik Lundh, on 2002-06-29, wrote:
>>=20
>> > or add the single character needed to make chomp a
>> > bit more robust:
>> >=20
>> >     def chomp(line):
>> >         if line[-1:] =3D=3D '\n':
>> >             line =3D line[:-1]
>> >         return line
>>=20
>> I think the fact that the first few tries here were *wrong*=20
>> about how to implement a chomp indicates that it really=20
>> should part of Python.  Just because it's easy to implement=20
>> doesn't mean it can't be part of the, say, string methods.  I=20
>> mean, look at str.{upper,lower,isdigit}, etc.  A chomp method=20
>> would be popular; rstrip or implementing your own doesn't cut it.
>
>I submitted a patch for this (that handled mac, win, and unix line
>endings) but it was declined by the BDFL. I suspect one of the reasons
>were that I named it chomp...
>
Try again with .stripEOL()
;-)

I think it's a worthy string method. I think it ought to do it based on the EOL
of the current platform by default.

'\n'.join(line.splitlines()) normalizes internal EOLs to \n and effectively chomps
so for a single line it seems to have the functionality, though slow.

Regards,
Bengt Richter



More information about the Python-list mailing list