5.2 字符串编码转换
5.2.1 encode()方法以 encoding 指定的编码格式编码字符串
str.encode([encoding="utf-8"][errors="strict"])
str1 = "星期一星期二星期三星期四星期五星期六星期日"
byte1 = str1.encode('UTF-8')
print(byte1)
output:
b'xe6x98x9fxe6x9cx9fxe4xb8x80xe6x98x9fxe6x9cx9fxe4xbax8cxe6x98x9fxe6x9cx9fxe4xb8x89xe6x98x9fxe6x9cx9fxe5x9bx9bxe6x98x9fxe6x9cx9fxe4xbax94xe6x98x9fxe6x9cx9fxe5x85xadxe6x98x9fxe6x9cx9fxe6x97xa5'
5.2.2 decode()方法以 encoding 指定的编码格式解码字符串
bytes.decode([encoding="utf-8"][,errors="strict"])
byte1 = b'xe6x98x9fxe6x9cx9fxe4xb8x80xe6x98x9fxe6x9cx9fxe4xbax8cxe6x98x9fxe6x9cx9fxe4xb8x89xe6x98x9fxe6x9cx9fxe5x9bx9bxe6x98x9fxe6x9cx9fxe4xbax94xe6x98x9fxe6x9cx9fxe5x85xadxe6x98x9fxe6x9cx9fxe6x97xa5'
str1 = byte1.decode('GBK')
print(str1)
output:
星期一星期二星期三星期四星期五星期六星期日