Splitting a file from specific column content

MRAB python at mrabarnett.plus.com
Sun Jan 22 15:55:49 EST 2012


On 22/01/2012 19:58, Arnaud Delobelle wrote:
> On 22 January 2012 16:09, MRAB<python at 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")
> ]
>
[snip]
Missing commas! :-)



More information about the Python-list mailing list