str->bytes:encode编码
bytes->str:decode解码
|
字符串通过编码成为字节码,字节码通过解码成为字符串。 >>> text = '我是文本' >>> text '我是文本' >>> print(text) 我是文本 >>> bytesText = text.encode() >>> bytesText b'xe6x88x91xe6x98xafxe6x96x87xe6x9cxac' >>> print(bytesText) b'xe6x88x91xe6x98xafxe6x96x87xe6x9cxac' >>> type(text) <class 'str'> >>> type(bytesText) <class 'bytes'> >>> textDecode = bytesText.decode() >>> textDecode '我是文本' >>> print(textDecode) 我是文本 |
其中decode()与encode()方法可以接受参数,其声明分别为: bytes.decode(encoding="utf-8", errors="strict") str.encode(encoding="utf-8", errors="strict") |
其中的encoding是指在解码编码过程中使用的编码(此处指“编码方案”是名词),errors是指错误的处理方案。 |