Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!txtfeed1.tudelft.nl!tudelft.nl!txtfeed2.tudelft.nl!amsnews11.chello.com!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'mrab': 0.04; 'python': 0.08; 'from:addr:python': 0.09; 'lines:': 0.09; 'lost.': 0.09; 'sections': 0.13; 'subject:file': 0.13; '"w")': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'out?': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'wrote:': 0.16; '(but': 0.21; 'header:In-Reply-To:1': 0.22; 'end,': 0.23; 'url:mailman': 0.26; 'pass': 0.28; 'true,': 0.28; 'lines': 0.30; 'url:listinfo': 0.30; 'break': 0.32; 'header:User- Agent:1': 0.33; 'to:addr:python-list': 0.33; 'loop': 0.34; 'received:84': 0.34; 'reply-to:addr:python.org': 0.34; 'try:': 0.34; 'url:python': 0.36; 'but': 0.37; 'getting': 0.37; 'skip:o 20': 0.38; 'url:org': 0.39; 'subject:from': 0.39; 'except': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; '"3":': 0.84; '"5":': 0.84; 'subject:content': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=J8QoHXbS c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=FFsdPcU1_MIA:10 a=A2dWweJ9qr0A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=8-UEtt-D3mB8Iydwt2sA:9 a=QEXdDO2ut3YA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Sun, 22 Jan 2012 16:09:17 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Splitting a file from specific column content References: <4F1C2915.6050609@mrabarnett.plus.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327248722 news.xs4all.nl 6904 [2001:888:2000:d::a6]:59957 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19218 On 22/01/2012 15:39, Arnaud Delobelle wrote: > On 22 January 2012 15:19, MRAB wrote: > >> Here's a solution in Python 3: >> >> input_path = "..." >> section_1_path = "..." >> section_2_path = "..." >> section_3_path = "..." >> >> with open(input_path) as input_file: >> try: >> line = next(input_file) >> >> # Copy section 1. >> with open(section_1_path, "w") as output_file: >> while line[0]< "3": >> output_file.write(line) >> line = next(input_file) >> >> # Copy section 2. >> with open(section_2_path, "w") as output_file: >> while line[5]< "5": >> output_file.write(line) >> line = next(input_file) >> >> # Copy section 3. >> with open(section_3_path, "w") as output_file: >> while True: >> output_file.write(line) >> line = next(input_file) >> except StopIteration: >> pass >> -- >> http://mail.python.org/mailman/listinfo/python-list > > Or more succintly (but not tested): > > > sections = [ > ("3", "section_1") > ("5", "section_2") > ("\xFF", "section_3") > ] > > with open(input_path) as input_file: > lines = iter(input_file) > for end, path in sections: > with open(path, "w") as output_file: > for line in lines: > if line>= end: > break > output_file.write(line) > Consider the condition "line >= 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.