用于将数据存取到文件中:
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的取数据(包含函数的复杂元素)必须要在取的时候声明函数。。。