首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python不支持中文混合编码

Python不支持中文混合编码

作者头像
贰师兄TEN
发布于 2020-12-08 10:44:22
发布于 2020-12-08 10:44:22
1.4K0
举报
文章被收录于专栏:Revel WorldRevel World

Python不支持中文混合编码,也就是说一个Python程序中Unicode对象的encode函数只能使用一种编码,不能混合使用utf-8,gbk等编码

正常:

代码: #coding=utf-8 import os print u'中国'.encode('gbk') tt = unicode("中文1", "utf-8") print tt.encode('gbk') a = u"中文2" print a.encode('gbk') 结果输出: 中国 中文1 中文2

错误:

代码: #coding=utf-8 import os print u'中国'.encode('utf8') tt = unicode("中文1", "utf-8") print tt.encode('gbk') a = u"中文2" print a.encode('gbk') 结果: \xe4\xb8\xad\xe5\x9b\xbd \xd6\xd0\xce\xc41 \xd6\xd0\xce\xc42

Powered by Zoundry Raven

Technorati : Python, Unicode, 中文 Del.icio.us : Python, Unicode, 中文

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2008-10-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档