I have a python script (script 1) which generate a large python dictionary. This dictionary has to be read by an another script (script 2). Could any one suggest me the best way to write the python dictionary generated by script 1 and to be read by script 2. In past I have used cPickle to write and read such large dictionaries. Is there a beter way to do this?
Asked
Active
Viewed 4,470 times
2
-
4Another name for a "large dictionary" is a "database". – georg Aug 17 '12 at 06:31
2 Answers
8
shelve
will give you access to each item separately, instead of requiring you to serialize and deserialize the entire dictionary each time.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
4
If you want your dictionary to be readable by different types of scripts (i.e. not just Python), JSON is a good option as well.
It's not as fast as shelve
, but it's easy to use and quite readable to the human eye.
import json
with open("/tmp/test.json", "w") as out_handle:
json.dump(my_dict, out_handle) # save dictionary
with open("/tmp/test.json", "r") as in_handle:
my_dict = json.load(in_handle) # load dictionary

Stewart Macdonald
- 2,062
- 24
- 27

David Cain
- 16,484
- 14
- 65
- 75