Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19231

Re: Splitting a file from specific column content

References <e1f0636a-195c-4fbb-931a-4d619d5f0d18@g27g2000yqa.googlegroups.com> <4F1C2915.6050609@mrabarnett.plus.com> <CAJ6cK1YGS8VUxCHW6NFe7fGp8FgzBSb_eaQD=Ug-mv_b46HDVA@mail.gmail.com> <4F1C34AD.8090401@mrabarnett.plus.com>
Date 2012-01-22 19:58 +0000
Subject Re: Splitting a file from specific column content
From Arnaud Delobelle <arnodel@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4936.1327262289.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 22 January 2012 16:09, MRAB <python@mrabarnett.plus.com> wrote:
> On 22/01/2012 15:39, Arnaud Delobelle wrote:
[...]
>> 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.

Of course you're correct - my reply was too rushed.  Here's a
hopefully working version (but still untested :).

sections = [
    ("3", "section_1")
    ("5", "section_2")
    ("\xFF", "section_3")
]

with open(input_path) as input_file:
    line, lines = "", 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 >= end:
                    break
                output_file.write(line)

-- 
Arnaud

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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