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


Groups > comp.lang.python > #58832 > unrolled thread

Re: chunking a long string?

Started byZero Piraeus <z@etiol.net>
First post2013-11-08 15:09 -0300
Last post2013-11-08 15:09 -0300
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#58832 — Re: chunking a long string?

FromZero Piraeus <z@etiol.net>
Date2013-11-08 15:09 -0300
SubjectRe: chunking a long string?
Message-ID<mailman.2239.1383934613.18130.python-list@python.org>
:

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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web