encode and decode an array of strings code example

encode and decode an array of strings

1

1 Example

0
import numpy as np

# Create a One-Dimensional array of string
arr = np.array(['Bella', 'Tom', 'John', 'Kate', 'Amy', 'Brad'])

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

# Encode string array values
# The arr is the input array to be encoded
e = np.char.encode(arr, encoding='cp037')
print("\nEncoded...\n",e)

# To decode string array values that is already encoded, use the numpy.char.decode() method in Python Numpy
# The "encoding" parameter sets the name of the encode used while encoding
print("\nResult (decode)...\n",np.char.decode(e, encoding='cp037'))
by on

Other Code Examples