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


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

chunking a long string?

Started byRoy Smith <roy@panix.com>
First post2013-11-08 12:48 -0500
Last post2013-11-09 00:54 +0000
Articles 2 on this page of 22 — 6 participants

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


Contents

  chunking a long string? Roy Smith <roy@panix.com> - 2013-11-08 12:48 -0500
    Re: chunking a long string? wxjmfauth@gmail.com - 2013-11-08 12:43 -0800
      Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-09 07:53 +1100
      Re: chunking a long string? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-08 20:57 +0000
      Re: chunking a long string? Tim Chase <python.list@tim.thechases.com> - 2013-11-08 15:04 -0600
      Re: chunking a long string? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-08 21:06 +0000
      Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-09 08:04 +1100
      Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-09 08:17 +1100
      Re: chunking a long string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-09 00:46 +0000
        Re: chunking a long string? wxjmfauth@gmail.com - 2013-11-09 00:14 -0800
          Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-09 19:26 +1100
            Re: chunking a long string? Roy Smith <roy@panix.com> - 2013-11-09 09:37 -0500
              Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-10 02:02 +1100
                Re: chunking a long string? Roy Smith <roy@panix.com> - 2013-11-09 10:21 -0500
                  Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-10 02:30 +1100
                    Re: chunking a long string? Roy Smith <roy@panix.com> - 2013-11-09 10:35 -0500
              Re: chunking a long string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-09 15:37 +0000
                Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-10 09:14 +1100
                  Re: chunking a long string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-10 06:39 +0000
                    Re: chunking a long string? Chris Angelico <rosuav@gmail.com> - 2013-11-10 19:46 +1100
          Re: chunking a long string? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-09 10:13 +0000
    Re: chunking a long string? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-09 00:54 +0000

Page 2 of 2 — ← Prev page 1 [2]


#58925

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-09 10:13 +0000
Message-ID<mailman.2287.1383992044.18130.python-list@python.org>
In reply to#58914
On 09/11/2013 08:14, wxjmfauth@gmail.com wrote:

I'll ask again, please don't send us double spaced google crap.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#58890

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-11-09 00:54 +0000
Message-ID<527d87b5$0$29983$c3e8da3$5496439d@news.astraweb.com>
In reply to#58824
On Fri, 08 Nov 2013 12:48:12 -0500, 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?

What edge conditions? Should be trivially easy to loop and slice:

def grouper(string, size):
    i = 0
    while i <= len(string):
        yield string[i:i+size]
        i += size


But if you prefer, there is a recipe in the itertools documentation to 
solve this problem for you:

http://docs.python.org/2/library/itertools.html#recipes

It's short enough to reproduce here.

from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

grouper(your_string, 10, '')

ought to give you the results you want.


I expect (but haven't tested) that for strings, the slice version will be 
faster.


-- 
Steven

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web