Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save dictionary to file

Tags:

python

I have a file looking like this:

732772  scaffold-3  G   G   A
732772  scaffold-2  G   G   A
742825  scaffold-3  A   A   G
776546  scaffold-3  G   A   G
776546  scaffold-6  G   A   G

I'm interested in using column 2 as my key, and output in a way that: having a unique key, and assiociated with it values.

in other words, if name in column 2 occurs more than once, output it only once, therefore the output should be:

scaffold-3
732772   G  G   A
742825   A  A   G
776546   G  A   G
scaffold-2
732772   G  G   A
scaffold-6
776546   G  A   G

I wrote sth like this:

res = open('00test','r')
out = open('00testresult','w')

d = {}
for line in res:
    if not line.startswith('#'):
        line = line.strip().split()
        pos = line[0]
        name = line[1]
        call = line[2]
        father = line[3]
        mother = line[4]

        if not (name in d):
            d[name] = []
        d[name].append({'pos':pos,'call':call,'father':father,'mother':mother})

but I have no idea, how to output it in a way I described above.

Any help will be nice

EDIT:

This is fully working code, that solved the problem:

res = open('00test','r')
out = open('00testresult','w')

d = {}
for line in res:
    if not line.startswith('#'):
        line = line.strip().split()
        pos = line[0]
        name = line[1]
        call = line[2]
        father = line[3]
        mother = line[4]

        if not (name in d):
            d[name] = []
        d[name].append({'pos':pos,'call':call,'father':father,'mother':mother})

for k,v in d.items():
    out.write(str(k)+'\n')
    for i in v:
        out.write(str(i['pos'])+'\t'+str(i['call'])+'\t'+str(i['father'])+'\t'+str(i['mother'])+'\n')

out.close()
like image 656
Irek Avatar asked May 14 '26 13:05

Irek


1 Answers

Now that you have your dictionary, loop over the items and write to a file:

keys = ('pos', 'call', 'father', 'mother')

with open(outputfilename, 'w') as output:
    for name in d:
        output.write(name + '\n')
        for entry in d['name']:
            output.write(' '.join([entry[k] for k in keys]) + '\n')

You may want to use a collections.defaultdict() object instead of a regular dictionary for d:

from collections import defaultdict

d = defaultdict(list)

and remove the if not (name in d): d[name] = [] lines altogether.

like image 178
Martijn Pieters Avatar answered May 16 '26 04:05

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!