美文网首页python
python3.x TypeError: must be str

python3.x TypeError: must be str

作者: 风寒夜末 | 来源:发表于2018-09-02 00:39 被阅读201次

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' 标志打开文件。

相关文章

网友评论

    本文标题:python3.x TypeError: must be str

    本文链接:https://www.haomeiwen.com/subject/ggepwftx.html