Saving:
import json
with open('data.json', 'w') as fp:
json.dump(data, fp)
Supply extra arguments like sort_keys or indent to get a pretty result. The argument sort_keys will sort the keys alphabetically and indent will indent your data structure with indent=N spaces.
json.dump(data, fp, sort_keys=True, indent=4)
Loading:
with open('data.json', 'r') as fp:
data = json.load(fp)
If you want to dump the JSON into a file/socket or whatever, then you should go for dump()
. If you only need it as a string (for printing, parsing or whatever) then use dumps()
(dump string). The functions with an s
take string parameters. The others take file streams.
Saving:
import cPickle as pickle
with open('data.p', 'wb') as fp:
pickle.dump(data, fp)
Loading:
with open('data.p', 'rb') as fp:
data = pickle.load(fp)