标签:author 反序 write and bsp logs ret load pickle
json序列化和json反序列化
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = ‘人生入戏‘ import json a = { "name":"test", "age":"20", } #json序列化 with open("json_test","w",encoding="utf-8") as f: f.write(json.dumps(a)) # 等同于这个 json.dump(a,f) #json反序列化 with open("json_test","r",encoding="utf-8") as f: print(json.loads(f.read())) #等同于这个 print(json.load(f))
pickle序列化和反序列化
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = ‘人生入戏‘ import pickle def hello(): print("Hello!") return 0 a = { "name":"test", "age":"20", "func":hello } with open("json_test","wb") as f: f.write(pickle.dumps(a)) #等同于这个 pickle.dump(a,f) with open("json_test","rb") as f: print(pickle.loads(f.read())["func"]()) #等同于这个 print(pickle.load(f)["func"]())
标签:author 反序 write and bsp logs ret load pickle
原文地址:http://www.cnblogs.com/my-times/p/7354617.html