[Chicago] is there really no built-in file/iter split() thing?

Damien Grassart damien at grassart.com
Sat Dec 1 22:19:28 CET 2007


On 12/1/07, skip at pobox.com <skip at pobox.com> wrote:
>
>         try:
>             line = f.next()
>         except StopIteration:
>             break


Bug: Eqivalent to f.read() if all your statements are on one line. ;)

I'd probably do something like this:

def chunker(f, sep=";", readsize=100):
    rest = ''
    while True:
        buf = f.read(readsize)
        if buf:
            chunks = (rest + buf).split(sep)
            for chunk in chunks[:-1]:
                yield chunk
            rest = chunks[-1]
        else:
            yield rest
            break

-Damien
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/chicago/attachments/20071201/f57c34ff/attachment.htm 


More information about the Chicago mailing list