python - "UnicodeDecodeError: ' charmap ' codec can' t decode"pickle 加载错误

标签 python encoding utf-8 pickle

我正在尝试在文件中保存一组 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/

相关文章:

python - 最长子串计算复杂度

python - 拆分多列

python - 哪个是执行命令的最佳 python 模块

.net - 在 Sql server 2005 中过滤 unicode 列 - 多数据匹配

c - Objective-C UTF-8 和 UTF-16 字符串(以字节表示)的差异

php - 在 Laravel 5 中获取数据库条目时的编码问题

Python写入文件

Python "string_escape"与 "unicode_escape"

php - JSON 类无法处理阿拉伯语单词

python - 尝试使用 Google Translate API 翻译 Discord Message 的结果只是组成输入的字母列表