numpy.defchararray.decode () in Python



numpy.core.defchararray.decode (arr, encoding) : This code function decodes a string (object) based on the specified codec.

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

Returns: Decoded string

Code:

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

import numpy as np 

 

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

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

  

print ( " arr1: " , arr1)

print ( "arr2:" , arr2)

 
# Using np.char.encode ()

encode_arr1 = np.char.encode (arr1, encoding < / code> = `cp037` )

print ( "Encoded arr1:" , encode_arr1)

  

encode_arr2 = np.char.encode (arr2, encoding = `utf8` )

print ( " Encoded arr2: " , encode_arr2)

 
# Using np.char.decode ()

decode_arr1 = np.char.decode (encode_arr1, encoding = `cp037` )

print ( "Decoded arr1:" , decode_arr1)

 

decode_arr2 = np.char .decode (encode_arr2, encoding = `cp037` )

print ( "Decoded arr1:" , decode_arr2)

 

decode_arr2 = np.char.decode (encode_arr2, encoding = `utf8` )

print ( < code class = "string"> "Decoded arr1:" , decode_arr2)

Exit:

 arr1: [`eAAAa`,` ttttds`, `AAtAAt`] arr2: [` 11sf`, `sdsf2`,` 1111f2`] Encoded arr1: [b`x85xc1xc1xc1x81` b`xa3xa3xa3xa3x84xa2` b`xc1xc1xa3xc1xc1`s arf2`] Encoded arr1: b`11s arf2`] b`11srf2: b`11s eAAAa` `ttttds`` AAtAAt`] Decoded arr2: [`x91x91ËÃ`` ËÀËÃx16` `x91x91x91x91Ãx16`] Decoded arr2: [` 11sf` `sdsf2`` 1111f2`]