Writing dictionary to file

Stephen Kloder stephenk at cc.gatech.edu
Fri Sep 8 01:23:30 EDT 2000


Gustaf Liljegren wrote:

> Hi, I'm looking for an elegant way to write a dictionary to a file. I'm not
> sure which format is suitable, but I would like each pair on a row,
> separated by a space, comma, semi-colon or something. Perhaps like this:
>
> John john at foo.com
> Greg greg at foo.com
> Fred fred at foo.com
>
> Thanks in advance,
>

To write dictionary dict to file f:

from string import join
f.write(join(map(lambda x:"%s %s"%x,dict.items()),'\n'))

--
Stephen Kloder               |   "I say what it occurs to me to say.
stephenk at cc.gatech.edu       |      More I cannot say."
Phone 404-874-6584           |   -- The Man in the Shack
ICQ #65153895                |            be :- think.





More information about the Python-list mailing list