PersianGulf PersianGulf - 1 year ago 130
Python Question

how to split a unicode string into list

I have the following code:

stru = "۰۱۲۳۴۵۶۷۸۹"
strlist = stru.decode("utf-8").split()
print strlist[0]

my output is :


But when i use:

print strlist[1]

I get the following

IndexError: list index out of range

My question is, how can I
? Of course, remember I get my
from a
, consider it's a

Answer Source

The split() method by default splits on whitespace. Therefore, strlist is a list that contains the whole string in strlist[0], and one single element.

If you want a list with one element for each unicode codepoint you can do transform it into a list in different ways:

  • Function: list(stru.decode("utf-8"))
  • List comprension: [item for item in stru.decode("utf-8")]
  • Not convert at all. Do you really need a list? You can iterate over the unicode string just like over any other sequence type (for character in stru.decode("utf-8"): ...)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download