iterating over lines in a file

nobody no at bo.dy
Wed Jul 19 23:13:12 EDT 2000


David Bolen <db3l at fitlinxx.com>, in <upuo9h97v.fsf at ctwd0143.fitlinxx.com>:

[...]
> The basic Python approach to iteratiing over a file would be:
> 
>     while 1:
>         line = file.readline() if not line: break
>         (... operations to perform ...)

> Yes, it has a break in it (although I personally don't consider
> 'break' ugly - break and continue are often the most elegant way to
> handle flow), and yes, it seems clumsy to those of us used to
> assignments within expressions, but its the sort of thing you just
> acknowledge and move on - it's really not that big a deal.

true, it just irked me. being a python newbie i'm running into a lot of
little annoyances like this, nothing major or worth complaining much over,
but details that confuse me nonetheless. python doesn't really fit my
personal taste very well, though better than some languages, and there are
more important things to a language than matters of taste.

(fwiw, my preference for this stems mostly from a liking for brevity; the
one real "flaw" in all the python workarounds is verbosity. i tend to think
faster than i type, even faster than i read, and i like to take in the
concepts of a program at as close to the speed of my mind as possible.)

> There's also a FAQ on this common question, available at:
>     http://www.python.org/doc/FAQ.html#6.30

thank you most kindly, i had been wondering where the FAQ was hidden!




More information about the Python-list mailing list