python37-encode与decode

编码(encode) 与 解码 ( decode )

str >>> (encode) >>> bytes
bytes >>> (decode) >>> str

str.encode():

>>> print('hello'.encode('utf-8'))
b'hello'
>>> print('中国'.encode('utf-8'))
b'xe4xb8xadxe5x9bxbd'
>>> print('dadada'.encode('utf-8'))
b'dadada'
>>> print('dadada'.encode()) #默认utf-8
b'dadada' >>> print('中国'.encode()) b'xe4xb8xadxe5x9bxbd'

bytes.decode():

>>> b = b'xe4xb8xadxe5x9bxbd'
>>> b.decode() #默认utf-8
'中国'
>>> b.decode('utf-8')
'中国'
原文地址:https://www.cnblogs.com/Demo-simple/p/11155049.html