python3 中的Json與Pickle
阿新 • • 發佈:2017-10-10
print readline 取數據 序列 import odin lin nco amp
Json與Pickle :功能主要用於文件數據序列化操作
1:Json的存數據到文件:
import json info = { "name":"hjc", "age":22 } with open("a1.txt","w",encoding="utf-8") as f: f.write(json.dumps(info))
2.Json的取數據
import json with open("a1.txt","r",encoding="utf-8") as f: data = json.loads(f.readline()) print(data["age"])
3.Pickle的存數據
import pickle def func(): print("hello tomorrow!!!") info = { "name" : "hjc", "age" : 24, "hobby" : func } with open("a1.txt","wb") as f: f.write(pickle.dumps(info))
4.Pickle的取數據:
import pickle def func(): print("hello tomorrow!!!") with open("a1.txt","rb") as f: data = pickle.loads(f.read()) data["hobby"]()
*pickle 讀寫都是采用二進制,所以要使用‘rb‘ & ‘wb‘
#文章引用KaShing大神
python3 中的Json與Pickle