[Pythonmac-SIG] Line endings and readline()
Jack Jansen
jack@oratrix.nl
Tue, 05 Sep 2000 23:50:56 +0200
Recently, Chris Barker <cbarker@jps.net> said:
> Andrew Watson wrote:
> > Is there some easy way of convincing python to change what it expects as th
> e
> > end-of-line character, to deal with this sort of crodd-platform problem?
>
> No, and I wish there was.
There are some cases where it can be useful, but in general you should
leave this to whoever did the transfer from mac to unix or
vice-versa. After all Python isn't the only Unix program that'll have
trouble with Mac line endings: all unix programs will. FTP clients,
archivers, and remote file server packages will usually do the
conversion for you if you set them up correctly. And on the Mac this
should be a breeze because all files of type TEXT should be translated
(unix and windows programs will have to gues whether a file is text
based on the extension, so you'll have to do the correct setup there
yourself).
A quick trick for converting any known convention to \n convention is
the following (if you can afford reading your whole file into memory)
data = f.read()
data = string.replace(data, '\r\n', '\n')
data = string.replace(data, '\r', '\n')
It'll even work for confused files, as you can often find in webpages
and such that have been edited on multiple systems. The only thing it
won't handle is files with \n\r (which can happen if you send a file
back and forth between all three platforms and don't get the
conversion right), but this is unsolvable anyway.
--
Jack Jansen | ++++ stop the execution of Mumia Abu-Jamal ++++
Jack.Jansen@oratrix.com | ++++ if you agree copy these lines to your sig ++++
www.oratrix.nl/~jack | see http://www.xs4all.nl/~tank/spg-l/sigaction.htm