python dictionary to csv where each key is in seperate row and value in separate columns
<p>I am having trouble trying to output my dictionary to CSV file.  I have a Dictionary that contains as keys time dates and as values, companies pertaining to those dates which are in string format.  I tried looking through the website for identical question, but it doesn't really help my case.  I tried the following code and managed to get the key in first row, values in second column but thats not what i want.  </p>

<pre><code>import csv
with open('dict1.csv','w') as f:
    w = csv.writer(f,delimiter=',')
    for key,values in sorted(a.items()):
        w.writerow([key,values])
</code></pre>

<p>But this gives me a CSV file in following format:</p>

<pre><code>2009/01/02  ['AA' 'BB' 'AAPL'] etc
2009/01/03  ['AA' 'CC' 'DD' 'FF']
</code></pre>

<p>Hence I only have two columns.  But I want:</p>

<pre><code>2009/01/02  'AA' 'BB' 'AAPL'
2009/01/02  'AA' 'CC' 'DD'  'FF'
</code></pre>

<p>in 4 separate columns for first row and 5 for the second row respectively.<br>
I even tried </p>

<pre><code>for dates in sorted(a):
    w.writerow([date] + my_dict[date] )
</code></pre>

<p>But this gives me error saying unsupported operand types for + 'timestamp' and 'str'.  </p>

<p>Any help will be appreciated.  Thanks</p>