First, serialization
Purpose: To transform data from memory into a process that can be stored or transmitted
Cause: The program is executed in a series of state changes, this series of "state" in the programming language in the various data structures stored in memory, and memory can not permanently save data, power outage or restart, in memory about the time of the data will disappear all; In order to persist the in-memory data to a file, it is necessary to serialize the in-memory data to be written directly to the file.
Pros: 1. The status of the data can be saved permanently
2. Better cross-platform data interaction capabilities
Second, JSON and pickle
1.json instances
Dictionary serialization
Import jsondic={"name": "" "Luffy,"Age": Print(type (DIC)) #<class "dict" ># through JSON Serialization becomes a string format j=json.dumps (DIC)print(Type (j)) #<class "str" >
dic={"name":"Namy"," Age": 18}f=open ("Serializing Objects","W") F.write (j)#= = Json.dump (dic,f)F.close#----Deserialization of----#Importjsonf=open ("Serializing Objects") msg=json.loads (F.read ())#= = Msg=json.load (f)
Serialization of lists
list=["how","old","is"," you "]j=json.dumps (list)print(Type (j))
2.pickle
Serialization of
Import pickledic={"Sunday":"Sun","Sweet ":"cake"}j=pickle.dumps (DIC) # format the dictionary as "bytes"
Deserialization
Import picklef=open (" serialized object _pickle","RB") msg= Pickle.loads (F.read ()) #= = Msg=pickle.load (f)
Day18 JSON and Pickle