Moving from perl to python: questions

Bernhard Herzog herzog at online.de
Wed Feb 16 10:59:32 EST 2000


Gaetan Corneau <Gaetan_Corneau at baan.com> writes:

> You can simplify "chomp", too:

Well, there are at two bugs in this implementation:

> # I didn't try it, but it should work :)
> def chomp(lines):
>    "remove the end of line markers from array"
>    import os
>    nl_len = len(os.linesep)   # length of os dependent line seperator

os.linesep is irrelevant here. The line separator will always be '\n'.
The underlying C-library takes care of translating whatever platform
specific convention is used into newlines if you opened the file in text
mode (the default). 

If the lines were read from a file in binary mode you might get problems
with readline (or readlines) because they only recognise '\n' as the
line separator and therefore won't work correctly with e.g.
Mac-textfiles.

>    for i in range(0, len(lines)):
>     lines[i] = lines[i][:-nl_len]

The last line may not end in a newline.

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list