Idiomatic portable way to strip line endings?
Emile van Sebille
emile at fenx.com
Sun Dec 16 10:23:25 EST 2001
"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)
--
Emile van Sebille
emile at fenx.com
---------
More information about the Python-list
mailing list