标签:imp block 数据序列化 序列化 pytho port name json序列化 txt
json是Python,Java等语言通用的,但是只能序列化简单的数据类型
# Author:Li Dongfei
import json
info = {
"name":"dongfei",
"age":"24"
}
f = open("test.txt","w")
f.write(json.dumps(info))
f.close()
# Author:Li Dongfei
import json
f = open("test.txt","r")
data = json.loads(f.read())
print(data["name"])
只有Python内部支持pickle
# Author:Li Dongfei
import pickle
def f1():
print("hello")
info = {
"name":"dongfei",
"age":"24",
"func":f1
}
f = open("test2.txt","wb")
f.write(pickle.dumps(info)) # == pickle.dump(info,f)
f.close()
# Author:Li Dongfei
import pickle
def f1():
print("hello")
f = open("test2.txt","rb")
data = pickle.loads(f.read()) # == pickle.load(f)
print(data["func"]())
注意:虽然可以dump多次,但是只能load一次,所以只需要dump一次
标签:imp block 数据序列化 序列化 pytho port name json序列化 txt
原文地址:https://www.cnblogs.com/L-dongf/p/9886327.html