[Tutor] list of dicts <-> dict of lists?

Peter Otten __peter__ at web.de
Fri May 28 23:33:54 CEST 2010


David Perlman wrote:

> Oh, except one problem: the csv.DictWriter lets you tell it what order
> you want the columns output in.  With your version, they just show up
> in whatever order Python wants them.

That's not hard to fix:

>>> fieldnames = "abca"
>>> cols = [data[fn] for fn in fieldnames]
>>> writer.writerows(zip(*cols))
1,4,7,1
2,5,8,2
3,6,9,3

Peter



More information about the Tutor mailing list