문제

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 traceback:

IndexError: list index out of range

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

도움이 되었습니까?

해결책 2

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"): ...)

다른 팁

  1. You don't need to.

    >>> print u"۰۱۲۳۴۵۶۷۸۹"[1]
    ۱
    
  2. If you still want to...

    >>> list(u"۰۱۲۳۴۵۶۷۸۹")
    [u'\u06f0', u'\u06f1', u'\u06f2', u'\u06f3', u'\u06f4', u'\u06f5', u'\u06f6', u'\u06f7', u'\u06f8', u'\u06f9']
    

You can do this

list(stru.decode("utf-8"))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top