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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
shelve
will give you access to each item separately, instead of requiring you to serialize and deserialize the entire dictionary each time.
回答2:
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