使用numpy.load(‘xxx.npy’)數據時,報錯
UnicodeError: Unpickling a python object failed: UnicodeDecodeError
html
因爲默認編碼問題,形成沒法對數據解包:
encoding must be 'ASCII', 'latin1', or 'bytes'
因此在使用np.load()
時須要加入編碼選項:
data = np.load('mynpy.npy',encoding='latin1')
便可順利載入。python
若是使用python3讀取python2生成的npy就有可能產生編碼錯誤,在numpy的源碼裏有說明:git
# 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/87983922github