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


Groups > comp.lang.python > #58830

Re: chunking a long string?

References <9258483E-76BD-4D96-8C46-7262A95E3ED4@panix.com>
Date 2013-11-08 12:04 -0600
Subject Re: chunking a long string?
From Skip Montanaro <skip@pobox.com>
Newsgroups comp.lang.python
Message-ID <mailman.2237.1383933900.18130.python-list@python.org> (permalink)

Show all headers | View raw


> I have a long string (several Mbytes).  I want to iterate over it in manageable chunks (say, 1 kbyte each).

You don't mention if the string is in memory or on disk. If it's in memory:

>>> for i in range(0, len(s), 10):
...   print repr(s[i:i+10])
...
'this is a '
'very long '
'string'

If your string is on disk, just loop over an open file object, reading
your chunk size every pass of the loop.

Skip

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: chunking a long string? Skip Montanaro <skip@pobox.com> - 2013-11-08 12:04 -0600

csiph-web