Alex Rivera | Logout

Using DictWriter to write a subset of a dictionary's keys

Asked 2010-07-08T23:46:33.490
23

I wrote a function that serializes a list of dictionaries as a CSV file using the csv module, with code like this:

data = csv.DictWriter(out_f, fieldnames)
data.writerows(dictrows)

However, I sometimes want to write out to a file only a subset of each dictionary's keys. If I pass as fieldnames a subset of the keys that each dictionary has, I get the error:

"dict contains fields not in fieldnames"

How can I make it so that DictRows will write just a subset of the fields I specify to CSV, ignoring those fields that are in the dictionary but not in fieldnames?

Edit
Report

1 Answer

0

Changes to your code:

Forget Dictwriter, use ordinary writer.

Then loop over your list of dicts:

for d in dictrows:
    ordinary_writer.writerow([d[fieldname] for fieldname in fieldnames])

Use d.get(fieldname, "") instead of d[fieldname] if you don't want an exception if there is no entry in d for a fieldname.

Note to anonymous downvoters: This is doing what Alex's solution is doing under the hood (see Lib/csv.py) and doing it a bit better ... csv.py calls a function to get each row in a list, and the guts of that function is

return [rowdict.get(key, self.restval) for key in self.fieldnames]
answered 2010-07-09T00:02:38.350

Your Answer