Converting a dictionary of tuple arrays to a CSV
<p>I'm trying to convert a dictionary structured like this:</p>

<pre><code>{
    'AAA': [ ('col1', 1), ('col2', 2), ('col3', 3) ],
    'BBB': [ ('col2', 1), ('col3', 4) ],
    'CCC': [ ('col4', 7) ]
}
</code></pre>

<p>...into a csv structured like this:</p>

<pre><code>key  col1, col2, col3, col4
AAA  1     2     3
BBB        1     4
CCC                    7
</code></pre>

<p>To be specific, I don't know what the columns will be named, or which columns will need to be created, until runtime, with the exception of the <code>key</code> column, which corresponds directly to the keys. If data isn't supplied for a given column, then it is considered to be empty.</p>

<p>Is there a simple way to do this in Python? I'm trying to avoid excessively re-shuffling the data around into different structures, and all examples I've seen for numpy involves parallel lists. I'm open to using libraries such as numpy and pandas.</p>

<p>There isn't a simple way to do what you're asking for without processing your dictionary first.</p>

<p>Python has a csv library: <a href="https://docs.python.org/2/library/csv.html" rel="nofollow">https://docs.python.org/2/library/csv.html</a> but you have to have your data in the right format before using it.  Your best bet is the <a href="https://docs.python.org/2/library/csv.html#csv.DictWriter" rel="nofollow"><code>DictWriter</code></a> class which can take a dict as each row.  Your tuples can be easily converted to dicts, so all you need to be able to use this class is to get a list of the fieldnames (column names).</p>

<p>Here is how I printed your info into a csv:</p>

<pre><code>from csv import DictWriter

d = { 'AAA': [ ('c1', 1), ('c2', 2), ('c3', 3)],
      'BBB': [ ('c2', 1), ('c3', 4)],
      'CCC': [ ('c4', 7)]
    }

# convert dictionary of tuples into list of dictionaries
# and gather fieldnames at the same time
rows = []
fieldnames = set()
for k in d.keys():
    # a list of (k, v) tuples can be converted to a dict
    # but watch out for duplicate keys!
    tmp = dict(d[k])
    fieldnames.update(tmp.keys())
    tmp['key'] = k
    rows.append(tmp)

# add key to the front of the list, since sets are unordered
# you could sort the fieldnames however you want here
fieldnames = ['key'] + list(fieldnames)                                                                 

# open the file and write the csv
with open('out.csv', 'w') as csvfile:
    writer = DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for row in rows:
        writer.writerow(row)
</code></pre>

python csv dictionary