Code block literals

Lulu of the Lotus-Eaters mertz at gnosis.cx
Thu Oct 9 15:04:12 EDT 2003


"Vis Mike" <visionary25 at _nospam_hotmail.com> wrote previously:
|Something like this seems more logical to me:
|for line in file('input.txt').lines:
|    do_something_with(line)
|for byte in file('input.txt').bytes:
|    do_something_with(byte)

Well, it's spelled slightly differently in Python:

    for line in file('input.txt').readlines():
        do_something_with(line)

    for byte in file('input.txt').read():
        do_something_with(byte)

Of course, both of those slurp in the whole thing at once.  Lazy lines
are 'fp.xreadlines()', but there is no standard lazy bytes.

A method 'fp.xread()' might be useful, actually.  And taking a good idea
from Dave Benjamin up-thread, so might 'fp.xreadwords()'.  Of course, if
you were happy to write your own class 'File' that provided the extra
iterations, you'd only need to capitalize on letter to get these extra
options.

Yours, Lulu...

--
 mertz@  _/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: \_\_\_\_    n o
gnosis  _/_/             Postmodern Enterprises            \_\_
.cx    _/_/                                                 \_\_  d o
      _/_/_/ IN A WORLD W/O WALLS, THERE WOULD BE NO GATES \_\_\_ z e






More information about the Python-list mailing list