Writing dictionary to file

Steve Holden sholden at holdenweb.com
Fri Sep 8 18:23:52 CEST 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,
> 
> Gustaf

pprint would allow you to read the values you produced back in to
Python programs (as long as the structures aren't recursive), I
don't know whether that's a goal or not.  If not, you've had some
prerty concise suggestions already.

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
IDLE 0.5 -- press F1 for help
>>> import pprint
>>> d = {'steve': 'sholden at bellatlantic.net', 'gustaf': 'gustafl at algonet.se'}
>>> pp = pprint.PrettyPrinter(width=2)
>>> pp.pprint(d)
{'gustaf': 'gustafl at algonet.se',
 'steve': 'sholden at bellatlantic.net'}

regards
 Steve
-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/



More information about the Python-list mailing list