Disclaimer: This article is a blogger original article, welcome to reprint! First, please indicate the source and author in the text ~~~ https://blog.csdn.net/u014636245/article/details/91407029
When using numpy.load ( 'xxx.npy') data, error
UnicodeError: Unpickling a python object failed: UnicodeDecodeError
Solution
Since the default coding problem, cause can not unpack the data:
encoding must be 'ASCII', 'latin1', or 'bytes'
it is in use np.load()
when the need to add encoding options:
data = np.load('mynpy.npy',encoding='latin1')
you can load successfully.
the reason
If you use python3 read npy python2 generated is likely to produce coding errors, are described in numpy source in:
# https://github.com/numpy/numpy/blob/v1.16.1/numpy/lib/npyio.py#L287-L453
# line 317
encoding : str, optional
What encoding to use when reading Python 2 strings. Only useful when
loading Python 2 generated pickled files in Python 3, which includes
npy/npz files containing object arrays. Values other than 'latin1',
'ASCII', and 'bytes' are not allowed, as they can corrupt numerical
data. Default: 'ASCII'
# 编码方式只允许选择下面三个中的一个
# line 390
if encoding not in ('ASCII', 'latin1', 'bytes'):
# The 'encoding' value for pickle also affects what encoding
# the serialized binary data of NumPy arrays is loaded
# in. Pickle does not pass on the encoding information to
# NumPy. The unpickling code in numpy.core.multiarray is
# written to assume that unicode data appearing where binary
# should be is in 'latin1'. 'bytes' is also safe, as is 'ASCII'.
#
# Other encoding values can corrupt binary data, and we
# purposefully disallow them. For the same reason, the errors=
# argument is not exposed, as values other than 'strict'
# result can similarly silently corrupt numerical data.
raise ValueError("encoding must be 'ASCII', 'latin1', or 'bytes'")
ref:
numpy:https://docs.scipy.org/doc/numpy/reference/generated/numpy.load.html
https://blog.csdn.net/qq_36718092/article/details/87983922