【python】numpy数据load报错
使用numpy.load(‘xxx.npy’)数据时,报错
UnicodeError: Unpickling a python object failed: UnicodeDecodeError
解决办法
由于默认编码问题,造成无法对数据解包:
encoding must be 'ASCII', 'latin1', or 'bytes'
所以在使用np.load()
时需要加入编码选项:
data = np.load('mynpy.npy',encoding='latin1')
即可顺利载入。
原因
如果使用python3读取python2生成的npy就有可能产生编码错误,在numpy的源码里有说明:
# 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