1、json:用于string到list或者dict类型之间的转换。
pickle用于python特有的类型和python的数据类型间进行转换
json和pickle模块都提供了四个功能:dumps,dump,loads,load
2、json序列化和反序列化
import
#将json序列化
data = {
"name":""jiayou",
"age":"12"
}
f.open("tetx.txt","w")
f.write(json,dumps(data))
f.close()
#json反序列化
f.open("text.txt","r")
data = json.loads(f.read())
print(data["name"])
f.close()
3、pickle序列化和反序列化
#pickle序列化
import pickle
def test_try(name):
print("name",name)
data1 = {
"name":"jiayou"
"age":"12"
""test":test_try,
}
f.open("text.txt","wb")
f.write(pickle.dumps(data1))
f.close()
#pickle反序列化
def test_try(name):
print("name",name)
import pickle
f = open("tetx.txt","rb")
data = pickle.loads(f.read())
print(data["test"])
f.close()