将Python列表编码为UTF-8 [英] Encode Python list to UTF-8
本文介绍了将Python列表编码为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的python列表:
I have a python list that looks like that:
list = [u'a', u'b', u'c']
现在,我想用UTF-8对其进行编码.因此,尽管我应该使用:
Now I want to encode it in UTF-8. Therefore I though I should use:
list = list[0].encode("utf-8")
但是打印列表仅给出
a
表示列表的第一个元素.甚至没有列表了.我在做什么错了?
meaning the first element of the list. Not even a list anymore. What am I doing wrong?
推荐答案
>>> items = [u'a', u'b', u'c']
>>> [x.encode('utf-8') for x in items]
['a', 'b', 'c']
这篇关于将Python列表编码为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文