Python Array fromunicode() 方法使用给定 unicodestring 中的数据扩展数组。该数组必须是 unicode character[u] 数组类型,否则会引发值错误。
语法
以下是 Python Array fromunicode() 方法的语法 -
参数
此方法接受 string 作为参数。
返回值
此方法不返回任何值。
示例 1
以下是 Python Array fromunicode() 方法的基本示例 -
输出
以下是上述代码的输出 -
Array Before Appending : array('u', 'abcdef')
Array After Appending : array('u', 'abcdefxyz')
Array After Appending : array('u', 'abcdefxyz')
示例 2
在此方法中,如果数组的数据类型不是 unicode 字符串,则会引发值错误。
在这里,我们创建了一个 int 数据类型的数组,当我们尝试使用 unicode-string 进行扩展时,它会引发一个错误 -
输出
Array Elements Before Appending : array('i', [10, 20, 30, 40, 50])
Traceback (most recent call last):
File "E:\pgms\Arraymethods prgs\frombyte.py", line 19, in <module>
arr2.fromunicode(y)
ValueError: fromunicode() may only be called on unicode type arrays
Traceback (most recent call last):
File "E:\pgms\Arraymethods prgs\frombyte.py", line 19, in <module>
arr2.fromunicode(y)
ValueError: fromunicode() may only be called on unicode type arrays
示例 3
如果数组的数据类型不是 unicode 字符串,我们使用 frombytes(unicode string.encode()) 方法来附加 unicode 数据 -
输出
以下是上述代码的输出 -
Array Elements Before Appending : array('d', [19.6, 24.9, 54.8, 60.8, 49.5])
Array Elements After Appending : array('d', [19.6, 24.9, 54.8, 60.8, 49.5, 1.3591493138573403e+190])
Array Elements After Appending : array('d', [19.6, 24.9, 54.8, 60.8, 49.5, 1.3591493138573403e+190])