简体   繁体   中英

writing list of tuples within a dictionary to csv

I have a dictionary, 'allData' containing lists of tuples.

allData = {'Shirts': [(69.95, 1), (52.45, 2), (99.95, 3), (79.95, 4), (79.95, 5)],
           'Jeans': [(70.0, 1), (50.0, 2), (99.0, 3), (79.95, 4), (80.0, 5)]}

I would like to write each key and its elements to a csv file.

The code I have so far is as follows:

def writeCSV(path, filename,  d):
    filename = path + filename

    with open(filename, 'wb') as outfile:
        writer = csv.writer(outfile, delimiter='~')
        writer.writerow(d.keys())
        writer.writerows(izip_longest(*d.values()))

    print "write file complete"

writeCSV("C:\\Projects\\Output", "output.csv", allData)

This gives me the following output in excel, where Shirts and Jeans are columns A and B.

Shirts      Jeans
(69.95, 1)  (49.95, 1)
(52.45, 2)  (0.0, 2)
(99.95, 3)  (104.95, 3)
(79.95, 4)  (59.95, 4)
(79.95, 5)  (80.0, 5)

This is output I actually need, where Shirts, id, Jeans, id are Columns A, B, C, D respectively.

Shirts  id  Jeans   id
69.95   1   70.0    1
52.45   2   50.0    2
99.95   3   99.0    3
79.95   4   79.95   4
79.95   5   80.0    5

Any help is much appreciated.

This works:

import csv
import os

def writeCSV(path, filename,  d):
    filename = os.path.join(path, filename)
    col_names = list(d.keys())
    header = []
    for name in col_names:
        header.append(name)
        header.append('id')

    with open(filename, 'wb') as outfile:
        writer = csv.writer(outfile) # change delimiter with `delimiter=' '`
        writer.writerow(header)
        index = 0
        end = max([len(x) for x in d.values()])
        while index < end:
            line = []
            for name in col_names:
                try:
                    row_values = d[name][index]
                except IndexError:
                    row_values = [''] * len(col_names)
                line.extend(row_values)
            writer.writerow(line)
            index += 1

Using unequal sized data:

allData = {'Shirts': [(69.95, 1), (52.45, 2), (99.95, 3), (79.95, 4), (79.95, 5)],
           'Jeans': [(70.0, 1), (50.0, 2), (99.0, 3), (79.95, 4), (80.0, 5) , (80.0, 5)]}

writeCSV(os.getcwd(), "csv_output.csv", allData)

Content of csv_output.csv :

Jeans,id,Shirts,id
70.0,1,69.95,1
50.0,2,52.45,2
99.0,3,99.95,3
79.95,4,79.95,4
80.0,5,79.95,5
80.0,5,,

Note that the column for Jeans is longer because it has one more dataset in the input dictionary.

Another possible solution:

import os
import csv

from itertools import izip_longest

def writeCSV(path, filename, d):
    def flatten(l):
        return [t[i] for t in l for i in range(len(t))]

    filename = os.path.join(path, filename)

    with open(filename, 'wb') as outfile:
        writer = csv.writer(outfile, delimiter='~')
        keys = d.keys()
        writer.writerow(flatten(zip(keys, ['id'] * len(keys))))
        # Keep the values in the same order as keys
        values = [d[key] for key in keys]
        writer.writerows(flatten(row) for row in izip_longest(*values, fillvalue=('', '')))
        print "write file complete"

writeCSV("C:\\Projects\\Output", "output.csv", allData)

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