Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '"this': 0.03; 'string': 0.09; 'chunk': 0.09; 'iterate': 0.09; 'subject:string': 0.09; 'python': 0.11; '10000000': 0.16; 'chunk:': 0.16; 'chunks': 0.16; 'itertools': 0.16; 'manageable': 0.16; 'roy': 0.16; 'stringio': 0.16; 'true:': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'import': 0.22; 'header:User-Agent:1': 0.23; '(or': 0.24; 'header :In-Reply-To:1': 0.27; 'fri,': 0.33; 'could': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'received:209.85.216': 0.37; 'received:209': 0.37; 'nov': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'break': 0.61; 'content- disposition:inline': 0.62; 'anything.': 0.68; 'smith': 0.68; 'received:190': 0.69; 'received:190.163': 0.84; 'subject:long': 0.84; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=+SuRfNEs+SWjrBqT4JXWENHq4qz5sXj5QSyBKoT8Mn4=; b=V9jcTKfNLvmcEdURT7GWBMeCHJydwB76FXO/nzJufghkRqe/O77EOhqFHLFEYey4dI 0tEO1dmPQAzijbT5N2SCNwgKQyUu0vMAm0AfJevrOCrmDLbbqQktYM0txbAbnlVTaNS1 1hkf/nq9FPpui24zXGTTFhhNAwakJDuyI1ZVN+CubFJ8YgkNsdtqb4T+09RTh7wZ9tIa E5TFHnX8bK9RuDLnpI6SPoPNu94MbJFaUHK2+ZYcb3pqhVfSYSo+6DirN6nseKw/42pX LU9oMkwAcUdIUry7/c6VBd7zmQ3AoIqlEw1RnCGbfkm2AUVEA6UkMc7K/BF2gk199JB1 8Mkw== X-Gm-Message-State: ALoCoQnPFoHYeIjTw2NVQ2IzJ16wGNmK0ihnIZgv0zH7Yezcx9ZTlUQEGk4MIZSotooSruQAmDXu X-Received: by 10.236.81.237 with SMTP id m73mr6869620yhe.29.1383934178003; Fri, 08 Nov 2013 10:09:38 -0800 (PST) Date: Fri, 8 Nov 2013 15:09:33 -0300 From: Zero Piraeus To: python-list@python.org Subject: Re: chunking a long string? References: <9258483E-76BD-4D96-8C46-7262A95E3ED4@panix.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9258483E-76BD-4D96-8C46-7262A95E3ED4@panix.com> X-PGP-Key: http://etiol.net/pubkey.asc User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383934613 news.xs4all.nl 15886 [2001:888:2000:d::a6]:46337 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58832 : On Fri, Nov 08, 2013 at 12:48:12PM -0500, Roy Smith wrote: > I have a long string (several Mbytes). I want to iterate over it in > manageable chunks (say, 1 kbyte each). > > "this is a " > "very long " > "string" > > This seems like something itertools would do, but I don't see anything. You could use io.StringIO (or StringIO.StringIO in Python 2.x): from io import StringIO big_str = 'x' * 10000000 stream = StringIO(big_str) while True: chunk = stream.read(1024) if not chunk: break # process chunk -[]z. -- Zero Piraeus: ad referendum http://etiol.net/pubkey.asc