"UnicodeDecodeError: 'charmap' codec can't decode" error in pickle load
"UnicodeDecodeError: 'charmap' codec can't decode" error in pickle load
我正在尝试在文件中保存一组 Tweet 对象。 Tweet class 个实例包含 utf8 编码字符。你可以看到下面的代码:
class Tweet:
author='';
text='';
time='';
date='';
timestamp='';
with open('tweets.dat','wb') as f:
pickle.dump(all_tweets,f)
with open('tweets.dat') as f:
all_tweets = pickle.load(f)
当我 运行 代码时,它 returns pickle.load(f) 行上的一个异常说明:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 25: character maps to <undefined>
我的机器规格:
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016,
11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
在Python3中,pickle模块期望底层文件对象接受或returnbytes。您正确地以二进制模式打开文件进行写入,但未能以同样的方式进行读取。读取部分应该是:
with open('tweets.dat', 'rb') as f:
all_tweets = pickle.load(f)
参考:摘自 pickle.load(fd)
的文档:
...Thus file can be an on-disk file opened for binary reading, an io.BytesIO object, or any other custom object that meets this interface.
我正在尝试在文件中保存一组 Tweet 对象。 Tweet class 个实例包含 utf8 编码字符。你可以看到下面的代码:
class Tweet:
author='';
text='';
time='';
date='';
timestamp='';
with open('tweets.dat','wb') as f:
pickle.dump(all_tweets,f)
with open('tweets.dat') as f:
all_tweets = pickle.load(f)
当我 运行 代码时,它 returns pickle.load(f) 行上的一个异常说明:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 25: character maps to <undefined>
我的机器规格:
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
在Python3中,pickle模块期望底层文件对象接受或returnbytes。您正确地以二进制模式打开文件进行写入,但未能以同样的方式进行读取。读取部分应该是:
with open('tweets.dat', 'rb') as f:
all_tweets = pickle.load(f)
参考:摘自 pickle.load(fd)
的文档:
...Thus file can be an on-disk file opened for binary reading, an io.BytesIO object, or any other custom object that meets this interface.