On 22/01/2012 15:39, Arnaud Delobelle wrote:
On 22 January 2012 15:19, MRAB<pyt...@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)
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.
--
http://mail.python.org/mailman/listinfo/python-list