Pickle is a Python data interaction that serializes the data and reads it when needed
Serialization of Pickle
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pickledef Test (): print (' test ') info = { ' name ': ' John ', c2/> ' age ': $, ' sex ': ' Male ', ' func ': test}f = open (' Pickle.txt ', ' WB ') #pickle序列化生成的数据为二进制f. Write ( Pickle.dumps (Info)) #另一种写法: Pickle.dump (info,f) f.close ()
The resulting file
Some messy data.
Pickle deserialization
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pickledef Test (): print (' test ') print (' test_1 ') F = open ( ' Pickle.txt ', ' RB ') data = Pickle.loads (F.read ()) #另一种写法: data = Pickle.load (f) print (data) print (data[' func ') ())
Run results
Pickle serialization can not only store data, but also store functions
Pickle deserialization can call a function that is deserialized, as long as the function name is the same, the content does not have to be the same
Serialization of Python-pickle