import pickle d = {"name":"cmz","age":18} l = [1,2,4,5] pk = open("data.pkl","w") print(pickle.dumps(d))
输出是bytes
C:\Python35\python.exe D:/luffy/第二章/pickle测试.py b‘\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x12X\x04\x00\x00\x00nameq\x02X\x03\x00\x00\x00cmzq\x03u.‘ Process finished with exit code 0
pickle 序列化(写入文件)
d = {"name":"cmz","age":18} l = [1,2,4,5] pk = open("data.pkl", "wb") pickle.dump(d, pk)
pickle 反序列化(从文件读出)
# 读出文件 f = open("data.pkl", "rb") d = pickle.load(f) print(d) 输出的是 {‘name‘: ‘cmz‘, ‘age‘: 18}
json 支持的数据类型
str,int,tuple,list,dict,可跨平台
pickle 支持的数据类型
支持python里面的所有数据类型,只能在python中使用