Splitting a file from specific column content
Arnaud Delobelle
arnodel at gmail.com
Sun Jan 22 10:39:54 EST 2012
On 22 January 2012 15:19, MRAB <python at 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
More information about the Python-list
mailing list