Python learning path ----- serialization, python ----- serialization
The process of changing variables from memory to storage or transmission is called serialization. in Python, it is called pickling. In other languages, it is also called serialization, alling, flattening, and so on.
After serialization, The serialized content can be written to the disk or transmitted to another machine over the network.
In turn, it is called deserialization to re-read the variable content from the serialized object to the memory, that is, unpickling.
Python provides the pickle module for serialization.
In the pickle Module
Pickle. the dumps () method serializes any object into a bytes, and then you can write this bytes into a file or use pickle. dumps () directly serialize the object and write it into a file-like object
For example:
Import pickle
D = dict (name = 'bob', age = 20, score = 80)
Pickle. dumps (d)
---> Serialize object d
B '\ x80 \ x03} q \ x00 (X \ x03 \ x00 \ x00 \ x00ageq \ x01K \ x14X \ x05 \ x00 \ x00 \ x00scoreq \ x02KXX \ x04 \ x00 \ x00 \ x00nameq \ x03X \ x03 \ x00 \ x00 \ x00Bobq \ x04u.'
Or use pickle. dump ():
F=open('dump.txt ', 'wb ')
Pickle. dump (d, f)
F. close ()
Writing d directly to f
That is, pickle. dump (object, file-like) or pickle. dumps (object) serialize the object and write it into the file (file-like) [Memory ---> External Storage]
Pickle. load (file-like) reads the content in the file to the memory for deserialization
*** Json Module
*-* Serialize the objects in the memory and write them to the file.
Json. dumps (obj, defaullt = iterator) ---> obj is the object to be stored in the file. iterator is the function that converts the object to dict.
For example:
Class student (object ):
Def _ init _ (self, name, score, age ):
Self. _ name = name
Self. _ score = score
Self. _ age = age
@ Property
Def Name (self ):
Return self. _ name
@ Property
Def Score (self ):
Return self. _ score
@ Property
Def Age (self ):
Return self. _ age
Def student2dict (std ):
Return {
'Name': std. name,
'Score ': std. score,
'Age': std. age,
}
S = student ('hangzhou)
# Jd = json. dumps (s, default = student2dict) # The object whose jd is serialized in json can be stored in the (json) file.
Jd = json. dumps (s, default = lambda obj: obj. _ dict _) # The python object whose jd is serialized in json format can be stored in (json) files.
Print (jd)
# Writing serialized objects to (json) files
With open ('file _ js. json', 'w') as f:
F. write (jd)
*-* Read the json content to the memory.
# Deserialization
# If we want to deserialize JSON into a Student object instance,
# The loads () method first converts a dict object,
# Then, the input object_hook function converts dict to a Student instance:
Def dict2student (dts ):
Return student (dts ['name'], dts ['score '], dts ['age'])
F1 = open ('file _ js. json', 'rb ')
S = f1.read ()
F1.close ()
# Json. loads (s, object_hook = none) function parameter s must be of the basic type:
# Deserialize's '(a ''str '', ''bytes'' or ''bytearray''
# Instance containing a JSON document) to a Python object
# It cannot be filestream. (In the first place, I tried to pass f1 into s and reported an error of pai_^)
X = json. loads (s, object_hook = dict2student)
Print ('the content read from the json file is % s, % d, % d', x. Name, x. Score, x. Age)
The above are in the Liao teacher's website (https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000) learning, may not so deep, but are through their own practice out of the results, please give me a lot of advice
This is the first time I wrote a blog. I feel a little hard-working, but I believe that it will surely be rewarded if I stick to it!