关于python中编码和数据类型的有关问题
关于python中编码和数据类型的问题
import pickle as p
import sys
shoplistfile='shoplist.data'
shoplist=['apple','mango','carrot']
f=open(shoplistfile, 'r')
p.dump(shoplistfile,f)
f.close()
del shoplistfile
f=open(shoplistfile)
storelist=p.load(f)
print(storelist)
错误提示:
Traceback (most recent call last):
File "C:\Python33\test\storage_and_disstorage.py", line 6, in <module>
p.dump(shoplistfile,f)
TypeError: must be str, not bytes
Traceback (most recent call last):
File "C:\Python33\test\storage_and_disstorage.py", line 11, in <module>
storelist=p.load(f)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
在网上找了一下,是编码的问题,但是尝试了提供的几种解决办法都不能解决,另外学艺不精,太底层的东西又不是很懂,哎,还是劳烦高人给指点指点 感谢感谢 另外 我用的是python3.3 我觉得是应该跟版本有关
------解决方案--------------------
1. dump中的文件参数要可以以二进制方式写入,所以打开模式应为'wb'.
f=open(shoplistfile, 'wb')
2. load也是一样,以二进制方式打开。
f=open(shoplistfile, 'rb')
------解决方案--------------------
翻翻手册先...
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).
The optional protocol argument tells the pickler to use the given protocol; supported protocols are 0, 1, 2, 3. The default protocol is 3; a backward-incompatible protocol designed for Python 3.0.
Specifying a negative protocol version selects the highest protocol version supported. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.
The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, a io.BytesIO instance, or any other custom object that meets this interface.
If fix_imports is True and protocol is less than 3, pickle will try to map the new Python 3.x names to the old module names used in Python 2.x, so that the pickle data stream is readable with Python 2.x.
import pickle as p
import sys
shoplistfile='shoplist.data'
shoplist=['apple','mango','carrot']
f=open(shoplistfile, 'r')
p.dump(shoplistfile,f)
f.close()
del shoplistfile
f=open(shoplistfile)
storelist=p.load(f)
print(storelist)
错误提示:
Traceback (most recent call last):
File "C:\Python33\test\storage_and_disstorage.py", line 6, in <module>
p.dump(shoplistfile,f)
TypeError: must be str, not bytes
Traceback (most recent call last):
File "C:\Python33\test\storage_and_disstorage.py", line 11, in <module>
storelist=p.load(f)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
在网上找了一下,是编码的问题,但是尝试了提供的几种解决办法都不能解决,另外学艺不精,太底层的东西又不是很懂,哎,还是劳烦高人给指点指点 感谢感谢 另外 我用的是python3.3 我觉得是应该跟版本有关
------解决方案--------------------
1. dump中的文件参数要可以以二进制方式写入,所以打开模式应为'wb'.
f=open(shoplistfile, 'wb')
2. load也是一样,以二进制方式打开。
f=open(shoplistfile, 'rb')
------解决方案--------------------
翻翻手册先...
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).
The optional protocol argument tells the pickler to use the given protocol; supported protocols are 0, 1, 2, 3. The default protocol is 3; a backward-incompatible protocol designed for Python 3.0.
Specifying a negative protocol version selects the highest protocol version supported. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.
The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, a io.BytesIO instance, or any other custom object that meets this interface.
If fix_imports is True and protocol is less than 3, pickle will try to map the new Python 3.x names to the old module names used in Python 2.x, so that the pickle data stream is readable with Python 2.x.