参考文章:https://www.cnblogs.com/alex3714/articles/7550940.html
Python2并不会自动的把文件编码转为unicode存在内存里, 那就只能使出最后一招了,你自己人肉转。Py3 自动把文件编码转为unicode必定是调用了什么方法,这个方法就是,decode(解码) 和encode(编码)
1
2
|
UTF
-
8
-
-
> decode 解码
-
-
>
Unicode
Unicode
-
-
> encode 编码
-
-
> GBK
/
UTF
-
8
..
|
PY3 除了把字符串的编码改成了unicode, 还把str 和bytes 做了明确区分, str 就是unicode格式的字符, bytes就是单纯二进制啦。
python2:
#_*_coding:utf-8_*_
s = "路飞"
print s
s2 = s.decode("utf-8")
print(s2 )
print(type(s2)) #unicode
s3 = s2.encode("gbk")
print( s3 ) #gbk
print(type(s3))
s4 = s2.encode("utf-8")
print(s4 ,s3,s2)