readline() with arbitrary end of lines

Javier Bezos see.below at no.spam.es
Sun Sep 10 12:27:48 EDT 2000


Hello,

I'm using MacPython 1.5.2c1 to read several files with
the help of readline(). However, the files to be read can
have either Mac or Unix end of lines and it seems that only
Mac end of lines are recognized; changing linesep has no
effect at all.

Here is an example. With a little file named test.txt

  Line 1
  Line 2
  Line 3

and the following short script

  a = open("test.txt", "r")
  print a.readlines()

I get

  ['Line 1\012', 'Line 2\012', 'Line 3']

with Mac end of lines (right), but

  ['Line 1\015Line 2\015Line 3']

with Unix ones (wrong!).

Can Python detect and convert transparently different
end of lines (as Tcl or Java can)? If not, can Python read
until a certain text is found (as AppleScript can)?

Thanks a lot for the answers.
___________________________________________________________
Javier Bezos              | TeX y tipografia
jbezos at wanadoo dot es  | http://perso.wanadoo.es/jbezos/



More information about the Python-list mailing list