iterating over lines in a file
Konrad Hinsen
hinsen at cnrs-orleans.fr
Fri Jul 21 04:31:36 EDT 2000
cjc26 at nospam.cornell.edu (Cliff Crawford) writes:
> * Remco Gerlich <scarblac-spamtrap at pino.selwerd.nl> menulis:
> | >
> | > The easiest way:
> | >
> | > for line in file.readlines():
> | > block
> |
> | But that would read the whole thing into memory, and he doesn't want that.
ScientificPython has a special class for text files which allows this:
for line in TextFile('foo'):
...
It also permits the transparent handling of compressed files and URLs
(for reading only) and, under Unix, expands home directory names
(~user). I use this about everywhere where I read data from a text
file; it's nice to be able to use the most general file name notations
everywhere without much effort.
For those who don't want to install a complete package just for this
minor module: you can use it on its own perfectly well, just put the
file Scienticic/IO/TextFile.py somewhere on your PYTHONPATH.
--
-------------------------------------------------------------------------------
Konrad Hinsen | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron | Fax: +33-2.38.63.15.17
45071 Orleans Cedex 2 | Deutsch/Esperanto/English/
France | Nederlands/Francais
-------------------------------------------------------------------------------
More information about the Python-list
mailing list