NumPy decode()
函数
在本教程中,我们将介绍 Numpy 库的 char 模块的decode()
功能。
NumPy 中的decode()
功能用于根据指定的编解码器对输入字符串进行解码。可用编解码器集取自 Python 标准库。
该函数以元素方式调用str.decode
。
decode()
的语法:
使用该函数所需的语法如下:
numpy.char.decode(a, encoding=None, errors=None)
以上语法表明decode()
是 char 模块的函数,取 NumPy 库中的两个参数。
参数:
让我们讨论一下这个函数的参数:
a 该参数表示一个字符串数组
编码 这是一个可选的参数,表示编码的名称。
错误 此参数用于指定如何处理错误。
返回值:
decode()
函数将总是返回一个解码的字符串。
decode()
基本示例:
代码片段如下,我们将在其中使用decode()
函数:
import numpy as np
x = np.array(['aAaAaArt', ' aABbV ', 'abBABba'])
print("Input is :", x)
e = np.char.encode(x, encoding='cp500')
print("\nAfter encoding: ", e)
d = np.char.decode(e, encoding='cp500')
print("\nAfter decoding: ", d)
输入为:[' aaaaaaart ' ' aABbV ' ' abbba ']
编码后:【b ' \ x81 \ xc1 \ x81 \ xc1 \ xc1 \ x99 \ xa3 ' b ' @ \ x81 \ xc1 \ xc2 \ x82 \ xe5 @ ' b ' \ x81 \ x82 \ xc2 \ xc1 \ xc2 \ x82 \ x81 ']
解码后:[' aaaaaaaaaaart ' ' aABbV ' ' abbbb
摘要
在本教程中,我们学习了 Numpy 库的decode()
功能。我们已经介绍了如何使用它的语法和这个函数返回的值。