Python基础-编码转化

编码转换图

 

utf-8汉字默认3个字节
gbk汉字默认2字节
utf-8转换成gbk码:先将utf-8解码成unicode然后再将该unicode编码成gbk码
编码和解码:
编码:使用变量的decode方法,需要说明的是解码需要指定原来是什么编码
解码:使用变量的encode方法
注:
  1. 目前python3.x中的str将不再支持decode/encode方法
  1. python3移除了unicode类型
例:
    python 2.7编码转换方式:
temp = "编码测试"

temp_unicode = temp.decode("utf-8") #编码 需要指定原来是什么编码

temp_gbk = temp.encode("gbk") #解码

 

    python 3.x编码转换方式:
temp = "编码测试"

temp_gbk = temp.encode("gbk")    #在python 3.x中代码编写人员可以直接将utf-8转换成gbk而中间过程则有python虚拟机替代码编写者完成

 

总结:
对于python2.7

  utf-8 > gbk

  utf-8b解码成unicode编码gbk

对于python3.x

  utf-8编码成gbk

注:windows输出自动将unicode转换成控制台相应的编码
posted @ 2017-07-11 14:54  BetterManPeter  阅读(131)  评论(0编辑  收藏  举报