[Tutor] Writing a csv from a dictionary

Mark Tolonen metolone+gmane at gmail.com
Tue Jun 23 16:31:39 CEST 2009


"Kent Johnson" <kent37 at tds.net> wrote in message 
news:1c2a2c590906230415q351c7c74kebc591907ce0e9c9 at mail.gmail.com...
On Tue, Jun 23, 2009 at 1:08 AM, Mark Tolonen<metolone+gmane at gmail.com> 
wrote:

>> import csv
>>
>> dyc = {
>> 'a50' : ['textfield', 50, 40],
>> 'k77' : ['othertext', 60, 10]
>> }
>>
>> myfile = open('csv-test.csv', 'w')
>> mywriter = csv.writer(myfile, dialect='excel')
>>
>> for k,[a,b,c] in dyc.items():
>> mywriter.writerow([k,a,b,c])
>
>I think I would write this as
>for k, v in dyc.items(): # or iteritems()
>  mywriter.writerow([k] + v)
>
>That way it doesn't depend on the actual size of the value list.

I knew there was a more straightforward way, it just wouldn't come to me at 
1am.  ;^)

-Mark




More information about the Tutor mailing list