dict_a = {'a':1,
'b':2}
# 错误示范,mode='w'
with open('test.pickle', mode='w') as f:
pickle.dump(dict_a, f)
# 报错,因为mode='w',只能写str
# 正确示范,mode=‘wb’,以二进制写文件,啥都行
with open('test.pickle', mode='wb') as f:
pickle.dump(dict_a, f)
Python保存各种类型的数据都可以用mode=‘wb’。