當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python numpy.defchararray.encode()用法及代碼示例


numpy.core.defchararray.encode(arr,encoding):此nuympy函數根據指定的編解碼器對字符串(對象)進行編碼。

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

返回: Encoded string



代碼:

# Python Program illustrating  
# numpy.char.encode() method  
import numpy as np  
  
arr1 = ['eAAAa', 'ttttds', 'AAtAAt'] 
arr2 = ['11sf', 'sdsf2', '1111f2'] 
  
# Printing the array 
print ("\narr1:", arr1) 
print ("\narr2:", arr2) 
  
print ("\nEncoded arr1:\n", np.char.encode(arr1, encoding ='cp037')) 
print ("\nEncoded arr2:\n", np.char.encode(arr2, encoding ='utf8')) 
print ("\nEncoded arr2:\n", np.char.encode(arr2, encoding ='utf8',  
                                                 errors = 'strict'))

輸出:

arr1: ['eAAAa', 'ttttds', 'AAtAAt']
arr2: ['11sf', 'sdsf2', '1111f2']

Encoded arr1:
 [b'\x85\xc1\xc1\xc1\x81' b'\xa3\xa3\xa3\xa3\x84\xa2'
 b'\xc1\xc1\xa3\xc1\xc1\xa3']

Encoded arr2:
 [b'11sf' b'sdsf2' b'1111f2']

Encoded arr2:
 [b'11sf' b'sdsf2' b'1111f2']


相關用法


注:本文由純淨天空篩選整理自Mohit Gupta_OMG 大神的英文原創作品 numpy.defchararray.encode() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。