Encode string array values in Numpy

NumpyServer Side ProgrammingProgramming

To encode string array values, use the numpy.char.encode() method in Python Numpy. The arr is the input array to be encoded. The "encoding" parameter sets the name of the encode. The set of available codecs comes from the Python standard library, and may be extended at runtime. The type of the result will depend on the encoding specified.

The numpy.char module provides a set of vectorized string operations for arrays of type numpy.str_ or numpy.bytes_.

Steps

At first, import the required library −

import numpy as np

Create a One-Dimensional array of string −

arr = np.array(['zbellazz' 'zztoMzzz' 'zzjohnzz' 'zzkatEzz' 'zzamyzzz' 'zzbradzz'])

Displaying our array −

print("Array...\n",arr)

Get the datatype −

print("\nArray datatype...\n",arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

Get the number of elements of the Array −

print("\nElements in the Array...\n",arr.size)

To encode string array values, use the numpy.char.encode() method in Python Numpy. The arr is the input array to be encoded. The "encoding" parameter sets the name of the encode −

print("\nResult (encode)...\n",np.char.encode(arr, encoding='cp037'))

Example

import numpy as np

# Create a One-Dimensional array of string
arr = np.array(['zbellazz' 'zztoMzzz' 'zzjohnzz' 'zzkatEzz' 'zzamyzzz' 'zzbradzz'])

# Displaying our array
print("Array...\n",arr)

# Get the datatype
print("\nArray datatype...\n",arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# Get the number of elements of the Array
print("\nElements in the Array...\n",arr.size)

# To encode string array values, use the numpy.char.encode() method in Python Numpy
# The arr is the input array to be encoded
# The "encoding" parameter sets the name of the encode
print("\nResult (encode)...\n",np.char.encode(arr, encoding='cp037'))

Output

Array...
['zbellazzzztoMzzzzzjohnzzzzkatEzzzzamyzzzzzbradzz']

Array datatype...
<U48

Array Dimensions...
1

Our Array Shape...
(1,)

Elements in the Array...
1

Result (encode)...
[b'\xa9\x82\x85\x93\x93\x81\xa9\xa9\xa9\xa9\xa3\x96\xd4\xa9\xa9\xa9\xa9\xa9\x91\x96\x88\x95\xa9\xa9\xa9\xa9\x92\x81\xa3\xc5\xa9\xa9\xa9\xa9\x81\x94\xa8\xa9\xa9\xa9\xa9\xa9\x82\x99\x81\x84\xa9\xa9']
raja
Updated on 17-Feb-2022 11:03:28

Advertisements