python decode encode 解码与编码问题

python 解码与编码问题

1.decode 俗称解码把编码解码成unicode,例如一个字符串变量 str 是utf-8编码,使用str.decode('utf-8')  ,就是把utf-8编码解码成unicode编码。

2.encode 俗称编码把unicode编码成其他编码,例如一个字符串变量 str 是unicode编码,使用str.encode('gbk'),就是把unicode编码成gbk.

3.unicode 俗称万国码全世界通用。(相当于中介)

4.那么问题来了,utf-8编码如何转换成gbk编码。解决方案当然是找中介----->unicode万国码

例如:字符串变量str是utf-8编码,先转成unicode编码,在encode编码成gbk编码。

代码:str.decode('utf-8').encode('gbk')

5.反过来也是一样,先找中介。

.unicode

猜你喜欢

转载自www.cnblogs.com/u3cc/p/11440543.html