Importing a dicrionary from a file

Gordon McMillan gmcm at hypernet.com
Wed Jan 5 08:51:37 EST 2000


Black Mantis wrote:

[BTW, this is not comp.lang.python.announce material]

> I am a newbie, and i am working on a program that uses a really
> big dictionary. i was wondering if i could store the dictionary
> in a seperate file, let's say dict.dat. Now, at the beggining of
> the program i want to import that dictionary and get it ready,
> how would i do this? is there a way to read the whole input file
> into one variable?

Some simpler alternatives:

- save it with
  open('mydict','w').write(repr(dict))
- load it with
  dict = eval( open('mydict','r').read() )

- save it with
  open('mydict.py', 'w').write( "d=%s" % repr(dict) )
- load it with
  from mydict import d




- Gordon




More information about the Python-list mailing list