Python - 如何从具有特殊字符的字符串创建字母列表?

Python - how do I create a list of letters from a string with special characters?

我想根据 python 中的这个字符串创建一个字母列表:

string = 'utf✮fff'

我试过这个:

>>> string = "utf✮fff"
>>> print list(string)
['u', 't', 'f', '\xe2', '\x9c', '\xae', 'f', 'f', 'f']

但是它应该是这样的:

['u', 't', 'f', '\xe2\x9c\xae', 'f', 'f', 'f']

有人知道如何创建此输出吗?提前致谢!

您必须使用 unicode 字符串:

string = u'utf✮fff'
print list(string)

[u'u', u't', u'f', u'\u272e', u'f', u'f', u'f']


string = 'utf✮fff'
string = unicode(string)
print list(string)

[u'u', u't', u'f', u'\u272e', u'f', u'f', u'f']

请注意,在您的情况下,您必须设置

# coding: utf8

header.