python列表内容中文编码问题
时间:2011-11-27
来源:互联网
>>> li=['sd','你好']
>>> print li
['sd', '\xe4\xbd\xa0\xe5\xa5\xbd']
>>> li[1].decode('gbk')
u'\u6d63\u72b2\u30bd'
>>> li[1].decode('utf-8')
u'\u4f60\u597d'
>>> unicode(li[1],'gbk')
u'\u6d63\u72b2\u30bd'
>>> li[1].decode('gb2312')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 2-3: illegal multibyte sequence
请问如何才能将li中的内容显示成中文呢?项目需要即将li[1]与中文字符串比较,但是li[1]是乱码该如何解决呢?
>>> print li
['sd', '\xe4\xbd\xa0\xe5\xa5\xbd']
>>> li[1].decode('gbk')
u'\u6d63\u72b2\u30bd'
>>> li[1].decode('utf-8')
u'\u4f60\u597d'
>>> unicode(li[1],'gbk')
u'\u6d63\u72b2\u30bd'
>>> li[1].decode('gb2312')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
UnicodeDecodeError: 'gb2312' codec can't decode bytes in position 2-3: illegal multibyte sequence
请问如何才能将li中的内容显示成中文呢?项目需要即将li[1]与中文字符串比较,但是li[1]是乱码该如何解决呢?
作者: jyjyjy1987 发布时间: 2011-11-27
引用楼主 jyjyjy1987 的回复:
>>> li[1].decode('utf-8')
u'\u4f60\u597d'
>>> li[1].decode('utf-8')
u'\u4f60\u597d'
这个不是乱码.
作者: panghuhu250 发布时间: 2011-11-27
str.find(u'你好')可以在str这个string中查找中文
作者: libralibra 发布时间: 2011-11-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28