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: encode() function

numpy.core.defchararray.encode() function

Calls str.encode 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.encode(a, encoding=None, errors=None)

Parameter:

Name Description Required /
Optional
a [array_like of str or unicode]
encoding The name of an encoding. str, optional
errors Specifies how to handle encoding errors. str, 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.encode() function


>>> c = np.array(['aAaAaA', '  aA  ', 'abBABba'])
>>> c
array(['aAaAaA', '  aA  ', 'abBABba'],
    dtype='|S7')
>>> np.char.encode(c, encoding='cp037')
array(['\x81\xc1\x81\xc1\x81\xc1', '@@\x81\[email protected]@',
    '\x81\x82\xc2\xc1\xc2\x82\x81'],
    dtype='|S7')

Example-2: numpy.encode() function

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

Python - NumPy Code Editor:

Previous: decode()
Next: join()