欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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...

错误描述

在使用Python3pickle读取Python2pickle保存的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