Storing dictionaries for later use: JSON and Pickle
JSON
Saving:
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.
Loading:
json.dump() vs json.dumps(), json.load() vs json.loads()
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.
Pickle
Saving:
Loading: