我正在尝试在文件中保存一组 Tweet 对象。 Tweet 类实例包含 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)
当我运行代码时,它在 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
最佳答案
在 Python 3 中,pickle 模块期望底层文件对象接受或返回字节。您正确地以二进制模式打开文件进行写入,但未能以同样的方式进行读取。阅读部分应该是:
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.
关于python - "UnicodeDecodeError: ' charmap ' codec can' t decode"pickle 加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41258585/