chunking a long string?
z at etiol.net
Fri Nov 8 19:09:33 CET 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 "
> 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)
chunk = stream.read(1024)
if not chunk:
# process chunk
Zero Piraeus: ad referendum
More information about the Python-list