#!/usr/bin/python
import pickle
shoplist=['apple','mango','carrot']
f = open('c:\poem.txt','w')
pickle.dump(shoplist,f)
f.close()
del shoplist
f = open('c:\poem.txt','r')
storedlist = pickle.load(f)
import pickle
shoplist=['apple','mango','carrot']
f = open('c:\poem.txt','w')
pickle.dump(shoplist,f)
f.close()
del shoplist
f = open('c:\poem.txt','r')
storedlist = pickle.load(f)
print(storedlist)
执行上述程序时候报错:TypeError: must be str, not bytes
解决方法:
在使用open打开文件的时候,加个b
f = open('c:\poem.txt','wb‘)
f = open('c:\poem.txt','rb')
本文介绍了一个关于Python中使用pickle模块保存和加载列表时遇到的TypeError错误:'must be str, not bytes'。通过调整文件打开模式的方式解决了该问题,并提供了正确的代码示例。
1万+





