The Pickle module consists of the dumps () function, the loads () function, the dump () function, and the load () function.
#pickle. Dumps (' object ') #序列化对象, the return value cannot be read directly
#pickle. Dump (' object ', f) #序列化对象到文件中
#pickle. Loads (' object ') #反序列化对象
#pickle. Load (f) #从文件中反序列对象, return to the original object
ImportPickleobj= 123,"ABCDEDF",["AC", 123],{"Key":"value","Key1":"value1"}#pickle.dumps (' object ') #序列化对象, the return value cannot be read directly#pickle.dump (' object ', f) #序列化对象到文件中#pickle.loads (' object ') #反序列化对象#pickle.load (f) #从文件中反序列对象, return the original objectR1=pickle.dumps (obj)Print(R1) R2=pickle.loads (R1)Print(R2)#f = open (' db ', ' rb+ ') # ' DB ' is the file name#pickle.dump (obj, f)#f.close ()With Open ('DB','rb+') as F:pickle.dump (obj, f) with open ('DB','RB') as F:r3=pickle.load (f)Print(R3)
Note: The pickle is binary, so the file should be opened with a B, such as ' WB ' or ' RB ' if only open with W or R will be an error
Python Pickle Module