Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python Array fromunicode() Method



The Python arrayfromunicode() method extends the array with data from the given unicodestring. The array must be a typeunicode character[u] array otherwise avalue error is raised.

Syntax

Following is the syntax of Python arrayfromunicode() method −

array_name.fromunicode(s)

Parameters

This method accepts string as a parameter.

Return Value

This method does not return any value.

Example 1

Following is the basic example of Python arrayfromunicode() method −

import array as arrarr1=arr.array('u',['a','b','c','d','e','f'])print("Array Before Appending :",arr1)x='xyz'arr1.fromunicode(x)print("Array After Appending :",arr1)

Output

Following is the output of above code −

Array Before Appending : array('u', 'abcdef')Array After Appending : array('u', 'abcdefxyz')

Example 2

In this method, If the datatype of an array is anything other than aunicode string, it raises avalue error.

Here, we have created an array ofint datatype and when we try to extend with unicode-string it will raise anerror

import array as arrarr2=arr.array('i',[10,20,30,40,50])print("Array Elements Before Appending :",arr2)y='abc'arr2.fromunicode(y)print("Array Elements After Appending :",arr2)

Output

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

Example 3

If the datatype of an array is other thanunicode string, We usefrombytes(unicode string.encode()) method to append unicode data −

import array as arrarr3=arr.array('d',[19.6,24.9,54.8,60.8,49.50])print("Array Elements Before Appending :",arr3)y='abcdhghg'arr3.frombytes(y.encode())print("Array Elements After Appending :",arr3)

Output

Following is the output of the above code −

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])
python_array_methods.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp