Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'mrab': 0.04; 'lines:': 0.09; 'lost.': 0.09; 'received:209.85.214.174': 0.13; 'sections': 0.13; 'subject:file': 0.13; '"w")': 0.16; 'out?': 0.16; 'wrote:': 0.16; '(but': 0.21; 'header:In-Reply-To:1': 0.22; 'end,': 0.23; 'hopefully': 0.24; 'message-id:@mail.gmail.com': 0.28; 'true,': 0.28; 'lines': 0.30; 'correct': 0.31; 'version': 0.31; 'break': 0.32; 'received:209.85.214': 0.32; 'to:addr:python-list': 0.33; 'loop': 0.34; 'too': 0.34; 'but': 0.37; 'received:google.com': 0.37; 'getting': 0.37; 'received:209.85': 0.38; 'skip:o 20': 0.38; 'subject:from': 0.39; 'received:209': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'reply': 0.65; ':).': 0.84; 'subject:content': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=fzASVgiZDGq8U84mN4V3J8cQBvvt7UxEZCrUElpoFbw=; b=o+3DCYwiNcwUGr5LBs1sUrMwi2+lGsY9H45jvdQE6wkgkL5IjQ1nacOTCXlsSgtR07 Y/cwQ/LNC+a3d+633hm39A8ilbpNfgwKqwIe89uLx73DRKEBGoyRsTCD4SQSCoQvChKS kiflbKTI2L/x4zWYPOwwVE29ndRNhOVVB/Lew= MIME-Version: 1.0 In-Reply-To: <4F1C34AD.8090401@mrabarnett.plus.com> References: <4F1C2915.6050609@mrabarnett.plus.com> <4F1C34AD.8090401@mrabarnett.plus.com> Date: Sun, 22 Jan 2012 19:58:06 +0000 Subject: Re: Splitting a file from specific column content From: Arnaud Delobelle To: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327262289 news.xs4all.nl 6975 [2001:888:2000:d::a6]:50663 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19231 On 22 January 2012 16:09, MRAB wrote: > On 22/01/2012 15:39, Arnaud Delobelle wrote: [...] >> Or more succintly (but not tested): >> >> >> sections =3D [ >> =C2=A0 =C2=A0 ("3", "section_1") >> =C2=A0 =C2=A0 ("5", "section_2") >> =C2=A0 =C2=A0 ("\xFF", "section_3") >> ] >> >> with open(input_path) as input_file: >> =C2=A0 =C2=A0 lines =3D iter(input_file) >> =C2=A0 =C2=A0 for end, path in sections: >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 with open(path, "w") as output_file: >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for line in lines: >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if line>=3D end: >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 br= eak >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 output_file.writ= e(line) >> > Consider the condition "line >=3D end". > > If it's true, then control will break out of the inner loop and start > the inner loop again, getting the next line. > > But what of the line which caused it to break out? It'll be lost. Of course you're correct - my reply was too rushed. Here's a hopefully working version (but still untested :). sections =3D [ ("3", "section_1") ("5", "section_2") ("\xFF", "section_3") ] with open(input_path) as input_file: line, lines =3D "", iter(input_file) for end, path in sections: with open(path, "w") as output_file: output_file.write(line) for line in lines: if line >=3D end: break output_file.write(line) --=20 Arnaud