I’ve got a question concerning the csv-module in python. I just can’t seem to write a list of dictionaries to a csv file. It would be fantastic if you could help me spot the mistake in here.
I get a TypeError: sequence item 0: expected str instance, dict found
f = open("datadump.csv", 'wt')
try:
fieldnames = ['type', 'name', 'contact', 'address', 'tel', 'web', 'infos']
writer = csv.DictWriter(f, fieldnames=fieldnames, delimiter=';', extrasaction='raise')
writer.writerow(dict((fn,fn) for fn in fieldnames))
for entry in listd:
writer.writerow(entry)
finally:
f.close()
listd is a list of dictionary objects, a typical one of which looks like this:
{'web': '', 'tel': 'Tel.: 043 377 97 32', 'name': ' Kinderkrippe Doppelchnopf', 'contact': '', 'address': 'Rütistrasse 37d, 8134 Adliswil', 'infos': '', 'type': 'kita'}
Your code basically unmodified works for me. I think maybe you have something strange with
listd. Try the full code below.