简体   繁体   中英

Save a dictionary to a file Python

Say I got a dictionary like this:

Test = {"apple":[3,{1:1,3:5,6:7}],"banana":[4,{1:1,3:5,6:7,11:2}]}

Now I want to save this dictionary into a temporary file so that I can reconstruct the dictionary later. (I am doing external sorting XD)

Can any kind guy help me? I know there is a way to save it in csv format, but this one is a special kind of dictionary. Thx a lot.

In order to save a data structure to a file you need to decide on a serialization format. A serialization format takes an in-memory data structure and turns it into a sequence of bytes that can be written to a file. The process of turning that sequence of bytes back into a data structure is called deserialization .

Python provides a number of options for seralization with different characteristics. Here are two common choices and their tradeoffs:

  • pickle uses a compact format that can represent almost any Python object. However, it's specific to Python, so only Python programs can (easily) decode the file later. The details of the format can also vary between Python releases, so it's best to use pickle only if you will re-read the data file with the same or a newer version of Python than the one that created it. Pickle is able to deal with recursive data structures. Finally, pickle is inappropriate for reading data provided by other possibly-malicious programs, since decoding a pickled data structure can cause arbitrary code to run in your application in order to reconstruct complex objects.

  • json uses a human-readable text-based format that can represent only a small set of data types: numbers, strings, None , booleans, lists and dictionaries. However, it is a standard format that can be read by equivalent libraries in many other programming languages, and it does not vary from one Python release to another. JSON does not support recursive data structures, but it is generally safe to decode a JSON object from an unknown source except that of course the resulting data structure might use a lot of memory.

In both of these modules the dump function can write a data structure to a file and the load function can later recover it. The difference is in the format of the data written to the file.

The pickle module is quite convenient for serializing python data. It is also probably the fastest way to dump and reload a python data structure.

>>> import pickle
>>> Test = {"apple":[3,{1:1,3:5,6:7}],"banana":[4,{1:1,3:5,6:7,11:2}]}
>>> pickle.dump(Test, open('test_file', 'w'))
>>> pickle.load(open('test_file', 'r'))
{'apple': [3, {1: 1, 3: 5, 6: 7}], 'banana': [4, {1: 1, 3: 5, 11: 2, 6: 7}]}

For me, clearly, the best serializer is msgpack .

http://jmoiron.net/blog/python-serialization/

Same methods as the others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM