#!/usr/bin/env pythonpython
# -*- coding:utf-8 -*-json
# author: Changhua Gongide
'''函數
Json僅支持簡單的數據類型,不支持複雜類型的序列化,如函數utf-8
'''it
import jsonclass
info = {"name":"daidai", "salary":15000}test
# 序列化import
with open("test.txt", "w") as f:coding
# f.write(json.dumps(info))
json.dump(info,f) # 與上句徹底等價
# 反序列化
with open("test.txt", "r") as f:
# print(json.loads(f.read()))
print(json.load(f)) # 與上句徹底等價
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
'''
Pickle可序列化python中全部的數據類型,僅在Python中
例如pickle可序列化函數
'''
import pickle
def fun_print():
print("1......")
info = {"name":"daidai", "salary":15000, "func":fun_print}
# 序列化
with open("test.txt", "wb") as f: # 以二進制寫入
# f.write(pickle.dumps(info))
pickle.dump(info,f) # 與上句徹底等價
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# author: Changhua Gong
import pickle
def fun_print(s):
print("2......%s" % s)
# 反序列化
with open("test.txt", "rb") as f: # 以二進制讀取
rs = pickle.load(f)
print(rs)
rs["func"]("daidai") # 實際上這裏有func對應函數對應便可(僅需保證函數名一致便可),甚至可修改調用方式和函數體