欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python encode和decode的妙用

程序员文章站 2022-05-25 18:43:21
>>> "hello".encode("hex") '68656c6c6f' 相应的还可以 >>> '68656c6c6f'.dec...

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

相应的还可以

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

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

codec

aliases

operand type

purpose

base64_codec base64, base-64 byte string convert operand to mime base64
bz2_codec bz2 byte string compress the operand using bz2
hex_codec hex byte string convert operand to hexadecimal representation, with two digits per byte
idna   unicode string implements rfc 3490. new in version 2.3. see also encodings.idna
mbcs dbcs unicode string windows only: encode operand according to the ansi codepage (cp_acp)
palmos   unicode string encoding of palmos 3.5
punycode   unicode string implements rfc 3492. new in version 2.3.
quopri_codec quopri, quoted-printable, quotedprintable byte string convert operand to mime quoted printable
raw_unicode_escape   unicode string produce a string that is suitable as raw unicode literal in source code
rot_13 rot13 unicode string returns the caesar-cypher encryption of the operand
string_escape   byte string produce a string that is suitable as string literal in source code
undefined   any raise an exception for all conversions. can be used as the system encoding if no automatic coercion between byte and unicode strings is desired.
unicode_escape   unicode string produce a string that is suitable as unicode literal in source code
unicode_internal   unicode string return the internal representation of the operand
uu_codec uu byte string convert the operand using uuencode
zlib_codec zip, zlib byte string compress the operand using gzip