Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.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.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'mrab': 0.04; 'python': 0.08; 'lines:': 0.09; 'received:209.85.214.174': 0.13; 'sections': 0.13; 'subject:file': 0.13; '"w")': 0.16; 'wrote:': 0.16; '(but': 0.21; 'header:In-Reply-To:1': 0.22; 'end,': 0.23; 'url:mailman': 0.26; 'message-id:@mail.gmail.com': 0.28; 'lines': 0.30; 'skip:\xc2 20': 0.30; 'url:listinfo': 0.30; 'break': 0.32; 'received:209.85.214': 0.32; 'to:addr:python-list': 0.33; 'url:python': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.38; 'skip:o 20': 0.38; 'url:org': 0.39; 'subject:from': 0.39; 'received:209': 0.39; 'subject:: ': 0.39; '8bit%:8': 0.40; 'to:addr:python.org': 0.40; 'more': 0.61; '"3":': 0.84; '"5":': 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=zBDGA47vAf2VmMrgLoh7nbeLJks8ZodQ52bZ6YEFSnE=; b=kP4R/+l6RrVwy3WLYG5icFu8enLqBP+fuN1XoDjPDcj7LWrVQeqRfTQ7JsAecodehB Q+/eWzbThPUtl70Ej+kf0BuA9cQlB+WfhrU5uf4ZRPFRlCA3znZp+B6qO2syI3dmxKEn gh47scbVMyATrIF31mmQW4LHE4FUjhEr0CiSs= MIME-Version: 1.0 In-Reply-To: <4F1C2915.6050609@mrabarnett.plus.com> References: <4F1C2915.6050609@mrabarnett.plus.com> Date: Sun, 22 Jan 2012 15:39:54 +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: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1327246798 news.xs4all.nl 6860 [2001:888:2000:d::a6]:47105 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:19216 On 22 January 2012 15:19, MRAB wrote: > Here's a solution in Python 3: > > input_path =3D "..." > section_1_path =3D "..." > section_2_path =3D "..." > section_3_path =3D "..." > > with open(input_path) as input_file: > =C2=A0 =C2=A0try: > =C2=A0 =C2=A0 =C2=A0 =C2=A0line =3D next(input_file) > > =C2=A0 =C2=A0 =C2=A0 =C2=A0# Copy section 1. > =C2=A0 =C2=A0 =C2=A0 =C2=A0with open(section_1_path, "w") as output_file: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0while line[0] < "3": > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0output_file.write(= line) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0line =3D next(inpu= t_file) > > =C2=A0 =C2=A0 =C2=A0 =C2=A0# Copy section 2. > =C2=A0 =C2=A0 =C2=A0 =C2=A0with open(section_2_path, "w") as output_file: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0while line[5] < "5": > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0output_file.write(= line) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0line =3D next(inpu= t_file) > > =C2=A0 =C2=A0 =C2=A0 =C2=A0# Copy section 3. > =C2=A0 =C2=A0 =C2=A0 =C2=A0with open(section_3_path, "w") as output_file: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0while True: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0output_file.write(= line) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0line =3D next(inpu= t_file) > =C2=A0 =C2=A0except StopIteration: > =C2=A0 =C2=A0 =C2=A0 =C2=A0pass > -- > http://mail.python.org/mailman/listinfo/python-list Or more succintly (but not tested): sections =3D [ ("3", "section_1") ("5", "section_2") ("\xFF", "section_3") ] with open(input_path) as input_file: lines =3D iter(input_file) for end, path in sections: with open(path, "w") as output_file: for line in lines: if line >=3D end: break output_file.write(line) --=20 Arnaud