编码(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') '中国'