Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58832

Re: chunking a long string?

Date 2013-11-08 15:09 -0300
From Zero Piraeus <z@etiol.net>
Subject Re: chunking a long string?
References <9258483E-76BD-4D96-8C46-7262A95E3ED4@panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.2239.1383934613.18130.python-list@python.org> (permalink)

Show all headers | View raw


:

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: chunking a long string? Zero Piraeus <z@etiol.net> - 2013-11-08 15:09 -0300

csiph-web