Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'variable,': 0.07; '__future__': 0.09; 'generators': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:string': 0.09; 'valueerror:': 0.09; 'def': 0.15; 'delimited': 0.16; 'martelli': 0.16; 'more:': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:Processing': 0.16; 'wrote:': 0.16; 'processed': 0.18; 'memory': 0.21; 'wondered': 0.23; 'variable': 0.24; 'string': 0.26; 'separate': 0.28; 'import': 0.28; 'yield': 0.29; 'from:addr:web.de': 0.30; 'item,': 0.30; 'pattern': 0.30; 'separately.': 0.30; 'alex': 0.31; 'ps:': 0.32; 'break': 0.32; "what's": 0.33; 'to:addr:python-list': 0.33; 'like:': 0.34; 'limitations': 0.34; 'try:': 0.34; 'header:X -Complaints-To:1': 0.35; 'url:python': 0.36; 'url:pipermail': 0.37; 'run': 0.37; 'but': 0.37; 'could': 0.38; 'received:org': 0.38; 'url:org': 0.38; 'subject:: ': 0.39; 'header:Mime- Version:1': 0.39; "there's": 0.39; "couldn't": 0.39; 'to:addr:python.org': 0.39; 'might': 0.40; 'more': 0.60; 'relevant': 0.69; 'data?': 0.84; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Processing a large string Date: Fri, 12 Aug 2011 16:48:10 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b3bb.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313160461 news.xs4all.nl 23861 [2001:888:2000:d::a6]:55028 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11279 Peter Otten wrote: > goldtech wrote: >> Say I have a very big string with a pattern like: >> >> akakksssk3dhdhdhdbddb3dkdkdkddk3dmdmdmd3dkdkdkdk3asnsn..... >> >> I want to split the sting into separate parts on the "3" and process >> each part separately. I might run into memory limitations if I use >> "split" and get a big array(?) I wondered if there's a way I could >> read (stream?) the string from start to finish and read what's >> delimited by the "3" into a variable, process the smaller string >> variable then append/build a new string with the processed data? > PS: This has come up before, but I couldn't find the relevant threads... Alex Martelli a looong time ago: > from __future__ import generators > > def splitby(fileobj, splitter, bufsize=8192): > buf = '' > > while True: > try: > item, buf = buf.split(splitter, 1) > except ValueError: > more = fileobj.read(bufsize) > if not more: break > buf += more > else: > yield item + splitter > > if buf: > yield buf http://mail.python.org/pipermail/python-list/2002-September/770673.html