python 3: pickle加载数据出现UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 6: ordin

二次信任 提交于 2019-12-28 01:35:11
Traceback (most recent call last):
  File "/anaconda3/workspace_data/emoji_data/data/PsychExp/load_pk_file.py", line 27, in <module>
    datas = loadList(path)
  File "/anaconda3/workspace_data/emoji_data/data/PsychExp/load_pk_file.py", line 17, in loadList
    segContent = pickle.load(pkl_file)
  File "/anaconda3/lib/python3.7/pickle.py", line 1593, in _load
    encoding=encoding, errors=errors).load()
  File "/anaconda3/lib/python3.7/pickle.py", line 1085, in load
    dispatch[key[0]](self)
  File "/anaconda3/lib/python3.7/pickle.py", line 1210, in load_string
    self.append(self._decode_string(codecs.escape_decode(data)[0]))
  File "/anaconda3/lib/python3.7/pickle.py", line 1201, in _decode_string
    return value.decode(self.encoding, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 6: ordinal not in range(128)

出现编码错误
def loadList(path):
pkl_file = open(path, ‘rb’)
segContent = pickle.load(pkl_file)
pkl_file.close()
return segContent

将上述代码中的 segContent = pickle.load(pkl_file)
更改为
segContent = pickle.load(pkl_file, encoding='bytes')

参考:https://blog.csdn.net/qq_41185868/article/details/79039604

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!