When using numpy.load (‘xxx. NPY ‘) data, an error is reported
Unicode error: unpicking a python object failed: Unicode decodeerror
solution
Due to the problem of default encoding, the data cannot be unpacked:
encoding must be 'ASCII', 'Latin1', or 'bytes'
therefore, when using NP. Load()
, you need to add encoding options:
data = NP. Load ('mynpy. NPY', encoding = 'Latin1')
can be loaded successfully
reason
If you use python3 to read the NPY generated by python2, coding errors may occur. It is explained in the source code of 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'
# Only one of the following three encoding methods is allowed
# 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'")
Similar Posts:
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1:
- Error reporting and resolution of Python 3 using binascii method
- [Solved] Python error: EOFError: Ran out of input
- TypeError: strptime() argument 1 must be str, not bytes
- TypeError: the JSON object must be str, not ‘bytes’
- Python TypeError: file must have ‘read’ and ‘readline’ attributes
- [Solved] Importerror: DLL load failed while importing mtrand: the specified program could not be found.
- Python2.7 Install Numpy Error:is not a supported wheel on…
- How to use htmltestrunner.py to report importerror: no module named ‘stringio’ in Python 3
- [Solved] “import numpy as np” ImportError: No module named numpy