bsmagic

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, 中文

分类:

技术点:

相关文章:

  • 2021-06-24
  • 2022-12-23
  • 2021-11-25
  • 2021-09-10
  • 2021-09-10
  • 2021-09-25
  • 2021-12-28
  • 2022-12-23
猜你喜欢
  • 2021-09-10
  • 2021-09-29
  • 2022-12-23
  • 2021-06-08
  • 2021-10-12
  • 2022-02-23
  • 2021-08-24
相关资源
相似解决方案