python encode和decode的妙用

时间:2021-05-23

>>> "hello".encode("hex")
'68656c6c6f'

相应的还可以

>>> '68656c6c6f'.decode("hex")
'hello'

查了一下手册,还有这些codec可用

Codec

Aliases

Operand type

Purpose

base64_codecbase64, base-64byte stringConvert operand to MIME base64bz2_codecbz2byte stringCompress the operand using bz2hex_codechexbyte stringConvert operand to hexadecimal representation, with two digits per byteidnaUnicode stringImplements RFC 3490. New in version 2.3. See also encodings.idnambcsdbcsUnicode stringWindows only: Encode operand according to the ANSI codepage (CP_ACP)palmosUnicode stringEncoding of PalmOS 3.5punycodeUnicode stringImplements RFC 3492. New in version 2.3.quopri_codecquopri, quoted-printable, quotedprintablebyte stringConvert operand to MIME quoted printableraw_unicode_escapeUnicode stringProduce a string that is suitable as raw Unicode literal in python source coderot_13rot13Unicode stringReturns the Caesar-cypher encryption of the operandstring_escapebyte stringProduce a string that is suitable as string literal in python source codeundefinedanyRaise an exception for all conversions. Can be used as the system encoding if no automatic coercion between byte and Unicode strings is desired.unicode_escapeUnicode stringProduce a string that is suitable as Unicode literal in python source codeunicode_internalUnicode stringReturn the internal representation of the operanduu_codecuubyte stringConvert the operand using uuencodezlib_codeczip, zlibbyte stringCompress the operand using gzip

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章