w3resource

NumPy String: numpy.char.decode() function

numpy.char.decode() function

The numpy.char.decode() is used to decode the elements of a byte array into strings using a specified encoding.

The function is useful in -
Data preprocessing: When working with data containing byte arrays, it may be necessary to convert these arrays into string arrays for further analysis or manipulation. The numpy.char.decode() function can be used to decode byte arrays into strings using a specified encoding.
Text analysis: In natural language processing or text analysis tasks, you may encounter byte arrays that need to be decoded into human-readable strings. The numpy.char.decode() function can be used to convert these byte arrays into strings suitable for analysis.

Syntax:

numpy.char.decode(a, encoding=None, errors=None)

Parameters:

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

Example: Encoding and decoding strings in a NumPy array using a custom encoding

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\xc1@@'
 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.

In the above code, the numpy.char.encode() and numpy.char.decode() functions are used to demonstrate how to encode and decode strings in a NumPy array using a custom encoding ('cp037').

Example: Encoding and decoding a string using a custom encoding in NumPy

>>> 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')

In the above example, the numpy.char.encode() and numpy.char.decode() functions are used to demonstrate how to encode and decode a single string using a custom encoding ('cp500').

Python - NumPy Code Editor:

Previous: center()
Next: encode()



Follow us on Facebook and Twitter for latest update.