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


Groups > comp.lang.python > #58830

Re: chunking a long string?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <skip.montanaro@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string': 0.09; 'chunk': 0.09; 'iterate': 0.09; 'subject:string': 0.09; 'cc:addr:python- list': 0.11; 'cc:name:python list': 0.16; 'chunks': 0.16; 'from:addr:pobox.com': 0.16; 'from:addr:skip': 0.16; 'loop.': 0.16; 'manageable': 0.16; 'range(0,': 0.16; 'sender:addr:gmail.com': 0.17; '>>>': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'skip': 0.24; 'cc:2**0': 0.24; 'mention': 0.26; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'file': 0.32; 'open': 0.33; 'received:google.com': 0.35; 'object,': 0.36; 'subject:?': 0.36; 'subject:long': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=XkFVrJNZ1Th+/naDODvRMYR6bjduYypAITpsMI7s/fA=; b=W4ae05sL+dMesNZe+5P8dGZZJhhKxejrOZ9QSgAnZGAdMolImpCBU8VklPkg4zkUYq fMuy09UQrIHVtFBMZNLDOHmwhbgOxt5njihed1943XP1k3Ew6TJz5ErJn1wUaENAn/8/ zjuNuOSfcIxNODSMxDgSjRygOjZaiYWuHeqnPTmlbIqGBzvlCzZDG/tZjE47E9XlPOWE yb8IgEhI4rgShbdLO2Wi/8NpJn4chGZ5AspmWte3h/oPJS6feEtuKP6H3lKuoLMENTtP 7ab9aiGQynHCBTXozWgNOR02NNbXrp/gNZtTaKNb3zG6g7R6E8Ud4NQrV+QCjuVGIE0x iqBg==
MIME-Version 1.0
X-Received by 10.42.92.6 with SMTP id r6mr3524629icm.21.1383933895956; Fri, 08 Nov 2013 10:04:55 -0800 (PST)
Sender skip.montanaro@gmail.com
In-Reply-To <9258483E-76BD-4D96-8C46-7262A95E3ED4@panix.com>
References <9258483E-76BD-4D96-8C46-7262A95E3ED4@panix.com>
Date Fri, 8 Nov 2013 12:04:55 -0600
X-Google-Sender-Auth 5TbSsU99HYr7esaC_wZJMKP3M7A
Subject Re: chunking a long string?
From Skip Montanaro <skip@pobox.com>
To Roy Smith <roy@panix.com>
Content-Type text/plain; charset=UTF-8
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2237.1383933900.18130.python-list@python.org> (permalink)
Lines 15
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383933900 news.xs4all.nl 15989 [2001:888:2000:d::a6]:36788
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:58830

Show key headers only | 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