import pickle d = {"name":"cmz","age":18} l = [1,2,4,5] pk = open("data.pkl","w") print(pickle.dumps(d))
输出是bytes
C:Python35python.exe D:/luffy/第二章/pickle测试.py b'x80x03}qx00(Xx03x00x00x00ageqx01Kx12Xx04x00x00x00nameqx02Xx03x00x00x00cmzqx03u.' 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中使用