streaming a file object through re.finditer

Erik Johnson spam at nospam.org
Wed Feb 2 22:27:17 EST 2005


    Is it not possible to wrap your loop below within a loop doing
file.read([size]) (or readline() or readlines([size]),
reading the file a chunk at a time then running your re on a per-chunk
basis?

-ej


"Erick" <idadesub at gmail.com> wrote in message
news:1107396614.888869.94640 at f14g2000cwb.googlegroups.com...
> Ack, typo. What I meant was this:
> cat a b c > blah
>
> >>> import re
> >>> for m in re.finditer('\w+', file('blah')):
>
> ...   print m.group()
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: buffer object expected
>
> Of course, this works fine, but it loads the file completely into
> memory (right?):
> >>> for m in re.finditer('\w+', file('blah').read()):
> ...   print m.group()
> ...
> a
> b
> c
>





More information about the Python-list mailing list