Idiomatic portable way to strip line endings?
Tim Hammerquist
tim at vegeta.ath.cx
Sun Dec 16 10:49:37 EST 2001
Emile van Sebille <emile at fenx.com> graced us by uttering:
>
> "Tim Hammerquist" <tim at vegeta.ath.cx> wrote in message
> news:slrna1pbik.6jf.tim at vegeta.ath.cx...
>> I've been trying to figure out the canonical way to strip the line
>> endings from a text file.
>>
>> In general, I can use:
>>
>> line = line[:-1]
>> or
>> del line[-1:]
>>
>> to strip the last character off the line, but this only works on
>> operating systems that have a one-byte line separator like Unix/Linux
>> ('\n'). The Win32 line separator is 2-bytes ('\r\n'), so this
>> solution is not portable.
>
> How about:
>
> line.endswith(os.linesep)
What about it? I used it in code in the original post. I know it
exists and is my preferred method of _detecting_if_ a line ends in
the designated linesep for the host OS.
However, the rest of my original post raises questions regarding
actually _stripping_ the linesep.
Tim Hammerquist
--
"This is your life ... and it's ending on minute at a time."
-- Jack, "Fight Club"
More information about the Python-list
mailing list