Python基础-encode编码和decode解码

一、encode编码和decode解码

1.unicode是 utf-8,gbk,gb2312的父编码,这些子编码都能转换成unicode编码,然后转化成其他子编码

例如utf8可以转成unicode,再转gbk,但不能直接从utf8转gbk

2.python中有两个方法用来解码(decode)与编码(encode),解码是子编码转unicode,编码就是unicode转子编码

(1)代码

str_ = "孙二"
str_1 = str_.encode('gbk')
str_11 = str_1.decode('gbk')
print("以gbk格式对str_进行编码:", str_1)
print("以gbk格式对str_1进行解码:", str_11)
print("-------------------------------------------")

str_2 = str_.encode('gb2312')
str_22 = str_2.decode('gb2312')
print("以gb2312格式对str_进行编码:", str_2)
print("以gb2312格式对str_2进行解码:", str_22)
print("-------------------------------------------")

str_3 = str_.encode('utf-8')
str_33 = str_3.decode('utf-8')
print("以utf-8格式对str_进行编码:", str_3)
print("以utf-8格式对str_3进行解码:", str_33)
print("-------------------------------------------")

str_4 = str_3.decode()  # utf8格式解码为unicode后再编码为gbk格式,直接从utf8编码为gbk格式会报错
str_5 = str_4.encode('gbk')
print(str_4)
print(str_5)

(2)结果

猜你喜欢

转载自blog.csdn.net/qq_39620483/article/details/88218213