Serialization:
#!usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = "Samson"
Import Json,pickle
#json能用于其他语言中, only some simple data types, such as dictionaries, can be serialized, and pickle can only be used in Python to serialize all data types
def sayhi (name): #程序运行结束时会释放掉该内存
Print ("Name,", name)
info = {
"Name": "Alex",
"Age": 22,
"Func": sayhi# using JSON serialization is not possible, while serialization with Pickle is OK
}
f = open ("Test.text", "WB")
#print (Json.dumps (info))
#f. Write (Json.dumps (info)) #相当于json. Dump (info,f). * * Individual tried, if dump many times, then can not load () and loads (), preferably with dumps
F.write (Pickle.dumps (info)) #相当于pickle. Dump (info,f)
F.close ()
Deserialization:
#!usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = "Samson"
Import Json,pickle
def sayhi (name): #必须有该函数名, otherwise unsuccessful
Print ("Name,", name)
Print ("name2,", name)
f = open ("Test.text", "RB")
#data = Json.loads (F.read ()) #相当于data2 = Json.load (f)
Data2 = Pickle.loads (F.read ()) #相当于data2 = Pickle.load (f)
F.close ()
Print (data2["func"] ("Alex"))
Python:json & Pickle Data serialization