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


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

Re: chunking a long string?

Started byPeter Otten <__peter__@web.de>
First post2013-11-08 19:02 +0100
Last post2013-11-08 19:02 +0100
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? Peter Otten <__peter__@web.de> - 2013-11-08 19:02 +0100

#58829 — Re: chunking a long string?

FromPeter Otten <__peter__@web.de>
Date2013-11-08 19:02 +0100
SubjectRe: chunking a long string?
Message-ID<mailman.2236.1383933759.18130.python-list@python.org>
Roy Smith wrote:

> 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?

(x)range() can take care of the edges:

>>> s = "this is a very long string"
>>> def chunks(s, size):
...     for start in xrange(0, len(s), size):
...             yield s[start:start+size]
... 
>>> list(chunks(s, 10))
['this is a ', 'very long ', 'string']
>>> list(chunks(s, 5))
['this ', 'is a ', 'very ', 'long ', 'strin', 'g']
>>> list(chunks(s, 100))
['this is a very long string']

Or you use StringIO:

>>> from functools import partial
>>> from StringIO import StringIO
>>> list(iter(partial(StringIO(s).read, 5), ""))
['this ', 'is a ', 'very ', 'long ', 'strin', 'g']

And no, this need not be a one-liner ;)

[toc] | [standalone]


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


csiph-web