编码问题
Python3 的编码:
name = "中国" #unicode print(name.encode("utf-8")) print(name.encode("gbk")) #编码,会把uniocde转成相应编码的同时,把字符变成bytes print(name) print(type(n="gb2312") #默认以utf-8解释 print(f.read())
Python2 的编码:
# -*- coding:utf-8 -*- name = "中国" #utf-8 格式的编码 print "utf-8",len(name) # print [name.decode("utf-8")] # #你现在是什么编码, print "unicode",len(name.decode("utf-8") ) # #你看到的并不是uinicode , 你看到的是unicode-->你的终端屏幕的编码格式 gbk = name.decode("utf-8").encode("GBK") #写要转成的目标编码 print gbk print len(gbk.decode("GBK").encode("gb2312"))
------- END ------
原文:https://www.cnblogs.com/george92/p/12990706.html