str ----> bytes: encode 编码

bytes----> str: decode 解码

str.encode()

bytes.decode()

>>> a = '123'
>>> a.encode('utf-8') #字符串类型 ---> 字节类型
b'123'
>>> a
'123'
>>> b = a.encode('utf-8') >>> b b'123' >>> b.decode('utf-8') #字节类型 ---> 字符串类型 '123'

文本的转化:

>>> str1 = '这是文本'
>>> str1
'这是文本'
>>> str2 = str1.encode('utf-8')
>>> str2
b'\xe8\xbf\x99\xe6\x98\xaf\xe6\x96\x87\xe6\x9c\xac'
>>> str3 = str2.decode('utf-8')
>>> str3
'这是文本'

总结:

编码是将字符串转化为字节码。

解码是将字节码转化成字符串。

posted on 2018-03-17 18:06  hello_xiaoyu  阅读(186)  评论(0编辑  收藏  举报