FAQ
:

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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 28 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedNov 8, '13 at 5:48p
activeNov 10, '13 at 8:46a
posts28
users10
websitepython.org

People

Translate

site design / logo © 2022 Grokbase