What is serialization?
Serialization refers to converting the data type in memory into a string so that it can be stored on the hard disk or transmitted over the network to the remote, because the hard disk or network can only receive bytes when it is transmitted.
Why serialize?
We write programs that involve a wide variety of objects, data structures, which are often present in memory in the form of variables. These variables are also cleaned up when the program finishes running. But we sometimes want to be able to recover an object from the last time we write the program (such as the results in machine learning, which requires the program to run for a long time and costs too much for multiple runs), which requires us to persist the variable for storage. One way to do this is to convert a variable into some form of string to a file, using the way the file reads and writes, but it is awkward to control the storage format yourself. A better way is to persist the variables locally by serializing them.
Two modules for serialization:
-
- JSON, which is used to convert between strings and Python data types.
- Pickle, which is used to convert between Python-specific types and Python data types.
Json
ImportJsondata= {"K1":"v1","K2":"v2"}#json.dumps () converts data into a string that is known to all programming languages and writes it in a special formWith open ("Test.txt","W") as F:json_str=json.dump (data,f)#json.load () reads and deserializes a serialized string from a fileWith open ("Test.txt","R") as F:Print(Json.load (f))#The result is: {' K1 ': ' v1 ', ' K2 ': ' V2 '}#json.dumps () converts data into a string that is recognized by all programming languages in a special formJSON_STR1 =json.dumps (data)Print(JSON_STR1)#The result is: {"K1": "V1", "K2": "V2"}#Json.loads deserializing a serialized stringJSON_STR2 =json.loads (JSON_STR1)Print(JSON_STR2)#The result is: {' K1 ': ' v1 ', ' K2 ': ' V2 '}
JSON
Pickle
ImportPickledata= {"K1":"v1","K2":"v2"}#pickle.dumps () converts data to a string that only Python knows and writes to a file in a special formWith open ("Test1.txt","WB") as F:pickle_str=pickle.dump (data,f)#pickle.load () reads and deserializes a serialized string from a fileWith open ("Test1.txt","RB") as F:Print(Pickle.load (f))#The result is: {' K1 ': ' v1 ', ' K2 ': ' V2 '}#pickle.dumps () converts data into a string that is recognized by all programming languages in a special formPICKLE_STR1 =pickle.dumps (data)Print(PICKLE_STR1)#The result is: B ' \x80\x03}q\x00 (x\x02\x00\x00\x00k1q\x01x\x02\x00\x00\x00v1q\x02x\x02\x00\x00\x00k2q\x03x\x02\x00\x00 \x00v2q\x04u. '#Pickle.loads deserializing a serialized stringPICKLE_STR2 =pickle.loads (PICKLE_STR1)Print(PICKLE_STR2)#The result is: {' K1 ': ' v1 ', ' K2 ': ' V2 '}
Pickle
Comparison of the two
Json:
Advantages: Cross-language, small size
Cons: Only supported: Int|str\list\tuple\dict
Pickle
Pros: Designed for Python and supports all Python data types
Cons: Only python knows that the storage data occupies a large space
Python Common Module--json,pickle module