I am working on a project with an interface of python and an other program where. I need to import data from an excel file und use the data stored in an array for further use. The data of the excel file is pure text.
So far I managed to convert the data into a list of strings. Now I am struggeling by converting the list to an array, not using numpy. As for this project I am working on an interface which does not work with numpy, that's why I have to use the array module.
Here is the part of the relevant code:
from array import array
data_list = []
for i in ws.values:
data_list.append(i)
print(data_list)
data_array = array('u', data_list))
print(data_array)
The first lines of code are just working fine. The problem shows up in the line where I want to create the array. Doing this I want to use the variable for the list variable (data_list) for that I don't have to tipe all of the 50+ strings.
data_array = array('u', data_list))
Fere the following error occurs:
TypeError: array item must be unicode character
I could not find a unicode character browsing through the internet. How can I fix this problem? Or is there an other way to convert a list of strings to an array (NOT using NumPy)?
I am also wondering whether 'u' is the correct type to use here.
The error indicates that there are values in the list which are not unicode characters – these might be numbers but also empty strings or strings longer than one character.
>>> import array
>>> array.array("u", "Hello World!") # string of characters – fine
array('u', 'Hello World!')
>>> array.array("u", ["Hello", "World!"]) # list of strings – error!
TypeError: array item must be unicode character
>>> array.array("u", ["1", "2", "3", "4"]) # list of characters – fine
array('u', '1234')
>>> array.array("u", ["1", 2, 3, "4"]) # list of characters/numbers - error!
TypeError: array item must be unicode character
Convert all values to string. Depending on the expected data format, either create separate arrays per word or str.join
all words.
>>> data = ["Hello World!", "Dear", 5]
>>> array.array("u", '\n'.join(str(line) for line in data))
array('u', 'Hello World!\nDear\n5')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.