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


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

Re: chunking a long string?

Started bySkip Montanaro <skip@pobox.com>
First post2013-11-08 12:04 -0600
Last post2013-11-08 12:04 -0600
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? Skip Montanaro <skip@pobox.com> - 2013-11-08 12:04 -0600

#58830 — Re: chunking a long string?

FromSkip Montanaro <skip@pobox.com>
Date2013-11-08 12:04 -0600
SubjectRe: chunking a long string?
Message-ID<mailman.2237.1383933900.18130.python-list@python.org>
> 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

[toc] | [standalone]


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


csiph-web