python encode和decode的妙用

yipeiwu_com5年前Python基础

>>> "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 python 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 python 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 python 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

相关文章

Python中MySQL数据迁移到MongoDB脚本的方法

MongoDB简介 MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。 MongoDB 是一个介于关系数据库...

Python快速从注释生成文档的方法

Python快速从注释生成文档的方法

作为一个标准的程序猿,为程序编写说明文档是一步必不可少的工作,如何才能写的又好又快呢,下面我们就来详细探讨下吧。 今天将告诉大家一个简单平时只要注意的小细节,就可以轻松生成注释文档,也可...

Python一句代码实现找出所有水仙花数的方法

水仙花数是指一个 3位正整数,它的每个位上的数字的 3 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153) 下面用一句代码实现找出所有的水仙花数: 方法一: &g...

Python生成随机MAC地址

利用python代码生成一个随机的MAC地址,使用python网络编程时或可用上,如果使用scapy模块则可直接利用RandMAC()函数来生成MAC。 python 复制代码 代码如下...

python实现猜数字游戏(无重复数字)示例分享

复制代码 代码如下:import time, random class GuessNum:    def __init__(self): &nbs...