pickle报UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xff in position 0:
程序员文章站
2024-01-16 11:00:16
错误描述在使用Python3的pickle读取Python2的pickle保存的bin文件时报UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)代码python2使用pickle保存bin文件使用pickle读取图片,将图片保存为bin文件import picklepath = "test.jpg"lfw_bins = []issame_l...
错误描述
在使用Python3的pickle读取Python2的pickle保存的bin文件时报UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
代码
- python2使用pickle保存bin文件
使用pickle读取图片,将图片保存为bin文件
import pickle
path = "test.jpg"
lfw_bins = []
issame_list = []
with open(path, 'rb') as fin:
_bin = fin.read()
lfw_bins.append(_bin)
output = "save.bin"
with open(output, 'wb') as f:
pickle.dump((lfw_bins, issame_list), f, protocol=pickle.HIGHEST_PROTOCOL)
- python读取bin文件
bins, issame_list = pickle.load(open(path, 'rb'))
错误分析
在使用Python2做上面的操作时没有任何问题,当用Python3来读取bin文件时就报错,代码如下
bin_path = os.path.join(face_data_dir, "lfw.bin")
bins, issame_list = pickle.load(open(bin_path, "rb"))
通过错误信息提示,也不难看出是因为编码问题导致的错误,所以接下来我们对字符编码做一些设定就可以解决上面的问题
#bin文件中包含图片的数据
pickle.load(open(bin_path, "r"), encoding='bytes')
#bin文件中包含字符串数据
pickle.load(open(bin_path, "r"), encoding='latin1')
参考:https://*.com/questions/11305790/pickle-incompatibility-of-numpy-arrays-between-python-2-and-3
本文地址:https://blog.csdn.net/sinat_29957455/article/details/107187850
上一篇: 剪映app怎么使用创作脚本做视频特效?
下一篇: neo4j在线增量批量插入三元组
推荐阅读
-
pickle报UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xff in position 0:
-
RLException: Invalid <node> tag: ‘ascii‘ codec can‘t decode byte 0xe6报错
-
Anaconda中启动Python时的错误:UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 553
-
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xcb in position 260: ordinal not in range(128)
-
命令行调用python出现编码错误:UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x9a in position 140 完美解决!
-
Python2.7更新pip:UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
-
Python “'ascii' codec can't decode byte 0xb0i in position 1” 的解决方法
-
真正解决Windows下UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xff in position 0错误的方法
-
当在命令行中执行virtualenv venv时报此错误:'utf-8' codec can't decode byte 0xd5 in position 38: invalid continuation by
-
UnicodeDecodeError: 'utf-8' codec can't decode byte in position : invalid continuation byte