简体   繁体   中英

Python: Writing to a csv file from a 'list' of {(tuples): value} dicts

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.

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.

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)

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') .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM