Split single file into multiple files based on patterns

Peter Otten __peter__ at web.de
Wed Oct 24 04:17:39 EDT 2012


satyam wrote:

> I have a text file like this
> 
> A1980JE39300007 2732 4195 12.527000
> A1980JE39300007 3465 9720 22.000000
> A1980JE39300007 1853 3278 12.500000
> A1980JE39300007 2732 2732 187.500000
> A1980JE39300007 19 4688 3.619000
> A1980JE39300007 2995 9720 6.667000
> A1980JE39300007 1603 9720 30.000000
> A1980JE39300007 234 4195 42.416000
> A1980JE39300007 2732 9720 18.000000
> A1980KK18700010 130 303 4.985000
> A1980KK18700010 7 4915 0.435000
> A1980KK18700010 25 1620 1.722000
> A1980KK18700010 25 186 0.654000
> A1980KK18700010 50 130 3.199000
> A1980KK18700010 186 3366 4.780000
> A1980KK18700010 30 186 1.285000
> A1980KK18700010 30 185 4.395000
> A1980KK18700010 185 186 9.000000
> A1980KK18700010 25 30 3.493000
> 
> I want to split the file and get multiple files like A1980JE39300007.txt
> and A1980KK18700010.txt, where each file will contain column2, 3 and 4.
> Thanks Satyam

import os
from itertools import groupby
from operator import itemgetter

get_key = itemgetter(0)
get_value = itemgetter(1)

output_folder = "tmp"
with open("infile.txt") as instream:
    pairs = (line.split(None, 1) for line in instream)
    for key, group in groupby(pairs, key=get_key):
        path = os.path.join(output_folder, key + ".txt")
        with open(path, "a") as outstream:
            outstream.writelines(get_value(line) for line in group)

If you are running the code more than once make sure that you remove the 
files from the previous run first.




More information about the Python-list mailing list