关于python储存与取储存的问题,出现TypeError: 以及UnicodeDecodeError:
系统:win10
IDLE:spyder3.
脚本:(关于储存和取储存)
#!/usr/bin/python# Filename: pickling.py
import cPickle as p
#import pickle as p
shoplistfile = 'shoplist.data'
# the name of the file where we will store the object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
del shoplist # remove the shoplist
# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist
运行出现错误 TypeError: write() argument must be str, not bytes
(然后我百度查到说, f = file(shoplistfile, 'w')需要用'wb',以二级制的方式)
再次运行出现错误 UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
(提示storedlist = p.load(f),在这边的序列有问题)
有些摸不到头绪,不知道是哪里的原因,请问可以如何解决?
另外还有一个关于软件spyder的问题,运行这个脚本一次之后,spyder里面的脚本代码对于属性颜色消失了,全是白色,而且也无法是用运行功能,截图示意。
运行后
回答:
读文件也要二进制模式
f = file(shoplistfile, 'rb')
以上是 关于python储存与取储存的问题,出现TypeError: 以及UnicodeDecodeError: 的全部内容, 来源链接: utcz.com/a/156751.html