python_48_Python3中字符编码与转码

python3默认是Unicode,不用声明# -*- coding:utf-8 -*-,如果声明则是utf-8
unicode='你好'
print('utf-8:',unicode.encode())#encode成utf-8,转码之后会变成byte类型
unicode_to_gbk=unicode.encode('gbk')#默认就是unicode,不用再decode,直接可以转成gbk
print(unicode_to_gbk)#打印gbk

gbk_to_utf8=unicode_to_gbk.decode('gbk').encode('utf-8')#gbk转成utf-8,与Python2相同
print('utf-8:',gbk_to_utf8)
print(gbk_to_utf8.decode('utf-8'))#decode一下byte字节格式就变成中文
gbk格式和gb2312字符相同,原因是gbk向下兼容gb18130,gb18130向下兼容gb2312

unicode='你好'.encode('utf-8')#python3中unicode转换成utf-8

  

 
posted @ 2018-01-21 20:07  耐烦不急  阅读(312)  评论(0编辑  收藏  举报