chunking a long string?

Zero Piraeus z at etiol.net
Fri Nov 8 13:09:33 EST 2013


:

On Fri, Nov 08, 2013 at 12:48:12PM -0500, Roy Smith wrote:
> I have a long string (several Mbytes).  I want to iterate over it in
> manageable chunks (say, 1 kbyte each).
> 
> "this is a "
> "very long "
> "string"
> 
> This seems like something itertools would do, but I don't see anything.

You could use io.StringIO (or StringIO.StringIO in Python 2.x):

    from io import StringIO
    big_str = 'x' * 10000000
    stream = StringIO(big_str)
    while True:
        chunk = stream.read(1024)
        if not chunk:
            break
        # process chunk

 -[]z.

-- 
Zero Piraeus: ad referendum
http://etiol.net/pubkey.asc



More information about the Python-list mailing list