chunking a long string?

Roy Smith roy at panix.com
Fri Nov 8 12:48:12 EST 2013


I have a long string (several Mbytes).  I want to iterate over it in manageable chunks (say, 1 kbyte each).  For (a small) example, if I started with "this is a very long string", and I wanted 10 character chunks, I should get:

"this is a "
"very long "
"string"

This seems like something itertools would do, but I don't see anything.  Is there something, or do I just need to loop and slice (and worry about getting all the edge conditions right) myself?

---
Roy Smith
roy at panix.com




More information about the Python-list mailing list