Python module - pickle

 The problem with Pickle, like all other programming language-specific serialization problems, is that it can only be used with Python, and possibly different versions of Python are not compatible with each other. Therefore, Pickle can only be used to save data that is not important, and cannot be successfully used. Deserialization doesn't matter.

#----------------------------Serialization
import pickle
 
dic={'name':'alvin','age':23,'sex':'male'}
 
print(type(dic))#<class 'dict'>
 
j=pickle.dumps(dic)
print(type(j))#<class 'bytes'>
 
 
f=open('serialized object _pickle','wb')#Note that w is written to str, wb is written to bytes, and j is 'bytes'
f.write(j) #------------------- Equivalent to pickle.dump(dic,f)
 
f.close()
#-------------------------Deserialization
import pickle
f=open('serialized object_pickle','rb')
 
data=pickle.loads(f.read())#  等价于data=pickle.load(f)
 
 
print(data['age'])  

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325014847&siteId=291194637