Previously asked a question Using defaultdict to parse multi delimiter file
While I do get the desired output based on the code, I am struggling to write it to a file as a table in this form
count pos _pos _neg
31022550
31022550
31022550
31022550
ids:
for key, rows in ids.iteritems():
for row in rows:
print '{}\t{}'.format(key, row)
31022550 {'count': '0', 'base': '=', 'pos': '20', '_neg': '0', '_pos': '0'}
31022550 {'count': '2', 'base': 'A', 'pos': '20', '_neg': '0', '_pos': '2'}
31022550 {'count': '0', 'base': 'C', 'pos': '20', '_neg': '0', '_pos': '0'}
31022550 {'count': '1391', 'base': 'G', 'pos': '20', '_neg': '672', '_pos': '719'}
31022550 {'count': '1', 'base': 'T', 'pos': '20', '_neg': '1', '_pos': '0'}
31022440 {'count': '0', 'base': 'N', 'pos': '20', '_neg': '0', '_pos': '0'}
31022550 {'count': '2', 'base': '+A', 'pos': '20', '_neg': '0', '_pos': '2'}
31022551 {'count': '0', 'base': '=', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551 {'count': '960', 'base': 'A', 'pos': '20', '_neg': '464', '_pos': '496'}
31022551 {'count': '0', 'base': 'C', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551 {'count': '13', 'base': 'G', 'pos': '20', '_neg': '9', '_pos': '4'}
31022551 {'count': '0', 'base': 'T', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551 {'count': '0', 'base': 'N', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551 {'count': '288', 'base': '+G', 'pos': '20', '_neg': '117', '_pos': '171'}
31022551 {'count': '9', 'base': '+GG', 'pos': '20', '_neg': '4', '_pos': '5'}
31022551 {'count': '1', 'base': '+GGG', 'pos': '20', '_neg': '0', '_pos': '1'}
Code
with open('mycsvfile.csv', 'w') as f:
writer = csv.writer(f)
for k, v in ids.iteritems():
writer.writerow([k] + v)
I would do this (python 2):
you need a double loop to iterate on the lists for each key. I have stored the column names in a list, so I can reuse it to build the rows in a list comprehension & for the title as well (first item doesn't have a title, I just left it blank)
now it looks like this:
(slightly shifted because tab character isn't wide enough, but not an issue to read it back)
Python 3 users would have to change:
by
and
by
note that the
writerow
double loop could be replaced by a single line, a double-loop, flat generator comprehension passed towriterows
, faster to execute: