编码和解码

在python2默认编码是ASCII ,python3里默认是unicode

#__author__ = ‘Shuaihu Mu‘

import sys
print(sys.getdefaultencoding())

s = ‘大家好‘  #这个是默认的Unicode
print (s.encode(‘utf-8‘))  #  unicode  -->  utf-8
print (s.encode(‘gbk‘))    #  unicode  -->  gbk
print (s.encode(‘gbk‘).decode(‘gbk‘).encode(‘gb2312‘))   #  unicode  -->  gbk  --> unicode  -->  gb2312
utf-8
b‘\xe5\xa4\xa7\xe5\xae\xb6\xe5\xa5\xbd‘
b‘\xb4\xf3\xbc\xd2\xba\xc3‘
b‘\xb4\xf3\xbc\xd2\xba\xc3‘

相关推荐