numpy.defchararray.encode () in Python

numpy.core.defchararray.encode (arr, encoding) : this nuympy function encodes a string (object) based on the specified codec.

Parameters:
arr: array-like or string.
encoding: [str] Name of encoding being followed.
error: Specifying how to handle error.

Returns: Encoded string

Code:

# Python program illustrating
# numpy.char.encode () method

import numpy as np 

 

arr1 = [ `eAAAa` , `ttttds` ​​ , ` AAtAAt` ]

arr2 = [ `11sf` , ` sdsf2` , `1111f2` ]

  
# Print array

print ( "arr1:" , arr1)

print ( "arr2:" , arr2 )

 

print ( " Encoded arr1: " , n p.char.encode (arr1, encoding = `cp037` ) )

print ( "Encoded arr2: " , np.char.encode (arr2, encoding = ` utf8 ` ))

print ( "Encoded arr2:" , np.char.encode (arr2, encoding = `utf8`

  errors = `strict` ))

Output:

 arr1: [`eAAAa`,` ttttds`, `AAtAAt`] arr2: [` 11sf`, `sdsf2`,` 1111f2`] Encoded arr1: [b`x85xc1xc1xc1x81` b`xa3xa3xa3xa3x84xa2` b`xc1xc1xa3xc1xc1xa3`] Encoded arr2: [b`11sf` b`sdsf2` b`111111f2`: Enco ]