简体   繁体   中英

How can I create an array of Unicode characters in Python?

I am using Repl.it.

import array as arr

my_array = arr.array("u", [u"3", u"6", u"9", u"12"])
print(my_array)
print(type(my_array))
print(type(my_array[0]))

The above source code produces the following error:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    my_array = arr.array("u", [u"3", u"6", u"9", u"12"])
TypeError: array item must be unicode character

Why the source code isn't working?

Array type "u" corresponds to a single Unicode character. The initializer contains a two-character item u"12" . Perhaps you want something like:

arr.array("u", [u"3", u"6", u"9", u"1", u"2"])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM