Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19216
| 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 | <arnodel@gmail.com> |
| 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 | <e1f0636a-195c-4fbb-931a-4d619d5f0d18@g27g2000yqa.googlegroups.com> <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 <arnodel@gmail.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4924.1327246798.27778.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 22 January 2012 15:19, MRAB <python@mrabarnett.plus.com> 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)
--
Arnaud
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Splitting a file from specific column content Yigit Turgut <y.turgut@gmail.com> - 2012-01-22 06:32 -0800
Re: Splitting a file from specific column content Roy Smith <roy@panix.com> - 2012-01-22 09:45 -0500
Re: Splitting a file from specific column content Roy Smith <roy@panix.com> - 2012-01-22 14:26 -0500
Re: Splitting a file from specific column content Tim Chase <python.list@tim.thechases.com> - 2012-01-22 13:34 -0600
Re: Splitting a file from specific column content Roy Smith <roy@panix.com> - 2012-01-22 14:37 -0500
Re: Splitting a file from specific column content Yigit Turgut <y.turgut@gmail.com> - 2012-01-22 12:16 -0800
Re: Splitting a file from specific column content MRAB <python@mrabarnett.plus.com> - 2012-01-22 15:19 +0000
Re: Splitting a file from specific column content Arnaud Delobelle <arnodel@gmail.com> - 2012-01-22 15:39 +0000
Re: Splitting a file from specific column content Yigit Turgut <y.turgut@gmail.com> - 2012-01-22 08:17 -0800
Re: Splitting a file from specific column content MRAB <python@mrabarnett.plus.com> - 2012-01-22 16:56 +0000
Re: Splitting a file from specific column content Yigit Turgut <y.turgut@gmail.com> - 2012-01-22 09:47 -0800
Re: Splitting a file from specific column content Eelco <hoogendoorn.eelco@gmail.com> - 2012-01-22 12:43 -0800
Re: Splitting a file from specific column content MRAB <python@mrabarnett.plus.com> - 2012-01-22 16:09 +0000
Re: Splitting a file from specific column content Arnaud Delobelle <arnodel@gmail.com> - 2012-01-22 19:58 +0000
Re: Splitting a file from specific column content MRAB <python@mrabarnett.plus.com> - 2012-01-22 20:55 +0000
csiph-web