Idiomatic portable way to strip line endings?

Erik Max Francis max at alcyone.com
Sun Dec 16 14:07:43 EST 2001


Skip Montanaro wrote:

> I suspect the high frequency of
> 
>     line = line[:-1]
> 
> simply means that most people haven't worried too much about their
> code
> running across multiple platforms.  I know for most of the code I've
> written
> I've never worried too much about it.

Not sure what the issue here is.  When a file is opened in text mode, as
I'd expect, looks like line endings on different platforms are
normalized to just one newline (presumably because Python is implemented
in C where this is true as well).

I just tested

	sys.stdin.readline()

and it indeed does _not_ have a trailing CR LF, just a trailing LF as
I'd expect.  So what's the issue here?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Laws are silent in time of war.
\__/ Cicero
    Esperanto reference / http://www.alcyone.com/max/lang/esperanto/
 An Esperanto reference for English speakers.



More information about the Python-list mailing list