Python Array fromunicode() Method



The Python array fromunicode() method extends the array with data from the given unicodestring. The array must be a type unicode character[u] array otherwise a value error is raised.

Syntax

Following is the syntax of Python array fromunicode() 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 array fromunicode() method −

import array as arr
arr1=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 a unicode string, it raises a value error.

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

import array as arr
arr2=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 than unicode string, We use frombytes(unicode string.encode()) method to append unicode data −

import array as arr
arr3=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
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy