0

如何将字符串如a = "我是中国人",转换成列表li =["我","是","中","国","人"]

a = "我是一个中国人"
li = list(a)
print li

输出却是

['\xe6', '\x88', '\x91', '\xe6', '\x98', '\xaf', '\xe4', '\xb8', '\x80', '\xe4', '\xb8', '\xaa', '\xe4', '\xb8', '\xad', '\xe5', '\x9b', '\xbd', '\xe4', '\xba', '\xba']

我用JavaScript很简单的就实现了

var a = "我是中国人"
li = a.split("")
console.log(li) // >>>["我","是","中","国","人"]

不知道python应该如何实现?

2017-04-27 提问

查看全部 8 个回答

7

已采纳

可以先将字符串解编码成unicode, 再用list

# 第一种:
>>> a = u"我是中国人"
>>> s = list(a)
>>> print s
[u'\u6211', u'\u662f', u'\u4e2d', u'\u56fd', u'\u4eba']
>>> print s[1]
是

# 第二种
>>> a = "我是中国人"
>>> s = a.decode('utf8')
>>> s = list(a.decode('utf8'))
>>> s
[u'\u6211', u'\u662f', u'\u4e2d', u'\u56fd', u'\u4eba']
>>> print s[1]
是

推广链接