mrruggle
mrruggle

Reputation: 5

Write dictionary of lists (varying length) to csv in Python

iam currently struggling with dictionaries of lists. Given a dictionary like that:

GO_list = {'Seq_A': ['GO:1234', 'GO:2345', 'GO:3456'],
   'Seq_B': ['GO:7777', 'GO:8888']}  

No i wanted to write this dictionary to a csv file as follows:

EDIT i have added the whole function to give more information

def map_GI2GO(gilist, mapped, gi_to_go):
with open(gilist) as infile:
    read_gi = csv.reader(infile)
    GI_list = {rows[0]:rows[1] for rows in read_gi} # read GI list into dictionary
    GO_list = defaultdict(list) # set up GO list as empty dictionary of lists
    infile.close()
with open(gi_to_go) as mapping:
    read_go = csv.reader(mapping, delimiter=',')         
    for k, v in GI_list.items(): # iterate over GI list and mapping file
        for row in read_go:
            if len(set(row[0]).intersection(v)) > 0 :
                GO_list[k].append(row[1]) # write found GOs into dictionary
                break
    mapping.close()
with open(mapped, 'wb') as outfile: # save mapped SeqIDs plus GOs
    looked_up_go = csv.writer(outfile, delimiter='\t', quoting=csv.QUOTE_MINIMAL)
    for key, val in GO_list.iteritems():
        looked_up_go.writerow([key] + val)
    outfile.close()

However this gives me the following output:

Seq_A,GO:1234;GO2345;GO:3456
Seq_B,GO:7777;GO:8888

I would prefer to have the list entries in separate columns,
separated by a defined delimiter. I have a hard time to get
rid of the ;, which are apparently separating the list entries.

Any ideas are welcome

Upvotes: 0

Views: 1373

Answers (1)

ZJS
ZJS

Reputation: 4051

If I were you I would try out itertools izip_longest to match up columns of varying length...

from csv import writer
from itertools import izip_longest

GO_list = {'Seq_A': ['GO:1234', 'GO:2345', 'GO:3456'],
'Seq_B': ['GO:7777', 'GO:8888']}

with open("test.csv","wb") as csvfile:
    wr = writer(csvfile)
    wr.writerow(GO_list.keys())#writes title row
    for each in izip_longest(*GO_list.values()): wr.writerow(each)

Upvotes: 1

Related Questions