python - 如何将unicode字符串拆分为列表

标签 python string unicode utf-8 unicode-string

<分区>

我有以下代码:

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

我的输出是:

۰۱۲۳۴۵۶۷۸۹

但是当我使用:

print strlist[1]

我得到以下 traceback:

IndexError: list index out of range

我的问题是,如何拆分我的字符串?当然,还记得我从 function 得到我的 string 吗,认为它是一个 variable 吗?

最佳答案

  1. 你不需要。

    >>> print u"۰۱۲۳۴۵۶۷۸۹"[1]
    ۱
    
  2. 如果您还想要...

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

关于python - 如何将unicode字符串拆分为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711384/

相关文章:

python - 如何在Python3中打印unicode列表

Eclipse 在重构过程中将日语变成垃圾

unicode - 将 GB2312 转换为 UTF-8

python - 使用最小元素比较对 5 个元素进行排序

python - OpenCV:如何规范化面部(去除阴影)?

c++ - 用 C++ 为非英语语言打印字符串

java - 该算法的时间复杂度: is it O(n^2) or O(n)

python - 使用求和索引作为推导顺序

python - 尝试使用 python 连接到 tcp 服务器时拒绝连接

java - 从末尾开始解析字符串