Tg.
Tg.

Reputation: 5784

How to open a large number of file with for loop?

I have tab-delimited file like this

tag1 DATA
tag20 DATA
tag4 DATA
tag3 DATA
tag10 DATA

This data has around 250000 lines. I want to sort these line into file by tag. Line begin with tag1 go into tag1.txt. Line begin with tag2 goto tag2.txt ... etc.

Is there any clever way to do this with loop ?

Upvotes: 0

Views: 372

Answers (2)

dheerosaur
dheerosaur

Reputation: 15172

This took 5 seconds for an input file with 200000 lines.

with open("input.txt") as f:
    for line in f:
        tag_fname, tag_data = line.split(' ',1)
        with open(tag_fname, 'a') as g:
            g.write(tag_data)

Upvotes: 1

mouad
mouad

Reputation: 70031

import collections

file_data = collections.defaultdict(list)

with open("you_file") as f
    for line in file:
        tag, data  = line.split('\t', 1)
        file_data[tag].append(data) 

for file_name, data in file_data.items():
    with open(file_name, 'w') as f:
         f.write("\n".join(data))

Upvotes: 2

Related Questions