美文网首页
python2 中列表中unicode转换为输出中文

python2 中列表中unicode转换为输出中文

作者: mysimplebook | 来源:发表于2020-07-21 15:00 被阅读0次

在python2脚本或命令行中,可以很容易将一个unicode字符串输出显示为中文。如

>>> a="德国少儿百科全书珍藏版:全辑".decode('utf-8')

>>> a

u'\u5fb7\u56fd\u5c11\u513f\u767e\u79d1\u5168\u4e66\u73cd\u85cf\u7248\uff1a\u5168\u8f91'

>>>

>>>

>>> print a

德国少儿百科全书珍藏版:全辑

但如果将a放入一个列表中,如

>>> print [a]

[u'\u5fb7\u56fd\u5c11\u513f\u767e\u79d1\u5168\u4e66\u73cd\u85cf\u7248\uff1a\u5168\u8f91']

>>>

这是为什么呢?[a]不是一个字符串,将原样输出。

我们可以将列表先转换为字符串。

>>> print str([a]).decode("unicode-escape")

[u'德国少儿百科全书珍藏版:全辑']

>>>

但此时,经过decode后,其类型变为unicode

>>> type(str([a]).decode("unicode-escape"))

<type 'unicode'>

>>>

相关文章

网友评论

      本文标题:python2 中列表中unicode转换为输出中文

      本文链接:https://www.haomeiwen.com/subject/ropikktx.html