python3.x TypeError: must be str, notbytesmust be str, not bytes
2016.07.02 11:09* 字数 117 阅读 0评论 0喜欢 2
importpickleasp
shoplistfile ='shoplist.data'
shoplist = ['apple','banana','carrot']
f =open(shoplistfile,'wb')
p.dump(shoplist, f)
f.close()
delshoplist
f =open(shoplistfile,'rb')
storelist = p.load(f)
print(storelist)
使用的python3.x,报错TypeError: must be str, not bytes,然后将open(shoplistfile,'w')和open(shoplistfile,'r')分别改成了open(shoplistfile,'wb')和open(shoplistfile,'rb'),就成功了额。
百度了下说是:pickle(除了最早的版本外)是二进制格式的,所以你应该带 'b' 标志打开文件。