[Tutor] Writing a csv from a dictionary

Eduardo Vieira eduardo.susan at gmail.com
Mon Jun 22 23:04:30 CEST 2009


Hello, I have a dictionary similar to this:
dyc = {
'a50' : ['textfield', 50, 40],
'k77' : ['othertext', 60, 10]
}

I was trying to write a csv with the csv module, by doing this:
import csv
myfile = open('c:/myscripts/csv-test.csv', 'wb')
mywriter = csv.writer(myfile, dialect='excel')

for item in dyc:
mywriter.write(item)

myfile.close()

this gives me this result:
a50,"['textfield', 50, 40]"
k77,"['othertext', 60, 10]"

I would like this, instead:
a50,"textfield", 50, 40
k77,"othertext", 60, 10

I also could manage to transform that dictionary in a list like this:
[ [a50,"textfield", 50, 40], [k77,"othertext", 60, 10]

Thanks in advance for your advice
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090622/41eea92f/attachment-0001.htm>


More information about the Tutor mailing list