Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

NumPy: decode() function

numpy.core.defchararray.decode() function

Calls str.decode element-wise.
The set of available codecs comes from the Python standard library, and may be extended at runtime.

Version: 1.15.0

Syntax:

numpy.core.defchararray.decode(a, encoding=None, errors=None)

Parameter:

Name Description Required /
Optional
a: array_like of str or unicode Required
encoding: str The name of an encoding. Optional
errors: str Specifies how to handle encoding errors. Optional

Return value:

out : ndarray

Note:

The 'chararray' class exists for backwards compatibility with Numarray, it is not recommended for new development. Starting from numpy 1.4, if one needs arrays of strings, it is recommended to use arrays of 'dtype' 'object_', 'string_' or 'unicode_', and use the free functions in the 'numpy.char' module for fast vectorized string operations.

Some methods will only be available if the corresponding string method is available in your version of Python.

The preferred alias for 'defchararray' is 'numpy.char'.

Example-1: numpy.char.decode() function

import numpy as np
x = np.array(['aAaAaA', '  aA  ', 'abBABba'])
print("x:", x)
e = np.char.encode(x, encoding='cp037')
print("\nAfter encoding: ", e)
d = np.char.decode(e, encoding='cp037')
print("\nAfter decodeing: ", d)

Output:

x: ['aAaAaA' '  aA  ' 'abBABba']

After encoding:  [b'\x81\xc1\x81\xc1\x81\xc1' b'@@\x81\[email protected]@'
 b'\x81\x82\xc2\xc1\xc2\x82\x81']

After decodeing:  ['aAaAaA' '  aA  ' 'abBABba'] 

Notes: The type of the result will depend on the encoding specified.

Example-2: numpy.char.decode() function

>>> import numpy as np
>>> a = np.char.encode('w3resource', 'cp500')
>>> b = np.char.decode(a,'cp500')
>>> b
array('w3resource', dtype='<U10')
>>> a
array(b'\xa6\xf3\x99\x85\xa2\x96\xa4\x99\x83\x85', dtype='|S10')

Python - NumPy Code Editor:

Previous: center()
Next: encode()