title: Python: Writing Nested Dictionary to CSV
body: <p>I'm trying to write a nested dictionary to a .csv file. Here is is a simple example:</p>
  
  <pre><code>import csv
  import itertools
  
  fields = [ 'org', '2015', '2014', '2013' ]
  dw     = { 'orgname1': { '2015' : 2, '2014' : 1, '2013' : 1 },
             'orgname2': { '2015' : 1, '2014' : 2, '2013' : 3 },
             'orgname3': { '2015' : 1, '2014' : 3, '2013' : 1 }
          }
  
  with open("test_output.csv", "wb") as f:
      w = csv.writer( f )
      years = dw.values()[0].keys()
      for key in dw.keys():
          w.writerow([key, [dw[key][year] for year in years]])
  </code></pre>
  
  <p>This gets me a table with two columns: the first contains <code>orgname</code>; the second contains [2, 1, 1] (or the corresponding values from the sub-dictionary). I'd like a table with four columns: one for <code>orgname</code> and then three for the corresponding list elements.</p>