Reputation: 2145
Greetings All.
I have a [(tuple): value] dicts as elements in a list as follows:
lst = [{('unit1', 'test1'): 11, ('unit1','test2'): 12}, {('unit2','test1'): 13, ('unit2','test2'):14 }]
testnames = ['test1','test2']
unitnames = ['unit1','unit2']
How to write to csv file with the following output?
unitnames, test1, test2
unit1, 11, 12
unit2, 13, 14
Thanks.
Upvotes: 2
Views: 2282
Reputation: 56654
The way you have lst grouped is redundant; all keys are unique, it might as well be a single dictionary, as
data = {
('unit1', 'test1'): 11,
('unit1', 'test2'): 12,
('unit2', 'test1'): 13,
('unit2', 'test2'): 14
}
then
import csv
def getUniqueValues(seq):
"Return sorted list of unique values in sequence"
values = list(set(seq))
values.sort()
return values
def dataArray(data2d, rowIterField=0, rowLabel='', defaultVal=''):
# get all unique unit and test labels
rowLabels = getUniqueValues(key[rowIterField] for key in data2d)
colLabels = getUniqueValues(key[1-rowIterField] for key in data2d)
# create key-tuple maker
if rowIterField==0:
key = lambda row,col: (row, col)
else:
key = lambda row,col: (col, row)
# header row
yield [rowLabel] + colLabels
for row in rowLabels:
# data rows
yield [row] + [data2d.get(key(row,col), defaultVal) for col in colLabels]
def main():
with open('output.csv', 'wb') as outf:
outcsv = csv.writer(outf)
outcsv.writerows(dataArray(data, 0, 'unitnames'))
if __name__=="__main__":
main()
and the output can easily be flipped (units across, tests down) by changing dataArray(data, 0, 'unitnames')
to dataArray(data, 1, 'testnames')
.
Upvotes: 1
Reputation: 798744
First, flatten the structure.
units = collections.defaultdict(lambda: collections.defaultdict(lambda: float('-inf')))
for (u, t), r in lst.iteritems():
units[u][t] = r
table = [(u, t['test1'], t['test2']) for (u, t) in units.iteritems()]
Then use csv
to write out the CSV file.
Upvotes: 4
Reputation: 10405
Following code creates a dict that can be dumped to a csv:
from collections import defaultdict
d = defaultdict(list)
for (unit, test), val in lst.items():
d[unit].append(val)
Upvotes: 1