Yet another reason to like Python
I was writing a small Python script the other day to convert plain text files to basic html (ok, don't tell me - I know I could probably have done it with one line of incomprehensible perl), and I had it working on AIX, but it wouldn't work on my Powerbook. Of course this turned out to be because of the difference between end-of-line characters between Unix, Macintosh, and Windows.
Turns out that there is a argument to the Python
file()command that will have it automatically take care of this problem:
"In addition to the standard fopen() values mode may be 'U' or 'rU'. If Python is built with universal newline support (the default) the file is opened as a text file, but lines may be terminated by any of '\n', the Unix end-of-line convention, '\r', the Macintosh convention or '\r\n', the Windows convention. All of these external representations are seen as '\n' by the Python program. If Python is built without universal newline support mode 'U' is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types seen. "
(from the current Python library reference documentation).
Cool!
While trying to track this down, I also came across Eric Raymond's May, 2000 Linux Journal article "Why Python?" - a good read.

Leave a comment