Python中报错TypeError: must be str, not bytes

#!/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)

print(storedlist)

执行上述程序时候报错:TypeError: must be str, not bytes


解决方法:

在使用open打开文件的时候,加个b

f = open('c:\poem.txt','wb‘)

f = open('c:\poem.txt','rb')



版权声明:本文为killvoon原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。