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 字符串大小写转换的简单实例

①所有字母都转换为大写 # -*- coding:utf-8 -*- if __name__ == "__main__":     a = 'hello,...

Python和perl实现批量对目录下电子书文件重命名的代码分享

经常会遇到下载的文件或电子书,名字中间都包含了一些网址信息,实际使用中由于名字太长不方便,下面的脚本使用正则表达式来对目录下的所有文件重命名: 例如: 修改前:[【听图阁-专注于Pyth...

python实现的用于搜索文件并进行内容替换的类实例

本文实例讲述了python实现的用于搜索文件并进行内容替换的类。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/python -O # coding: UTF-8 "...

在Python中使用异步Socket编程性能测试

OK,首先写一个python socket的server段,对开放三个端口:10000,10001,10002.krondo的例子中是每个server绑定一个端口,测试的时候需要分别开3...

Python基础学习之时间转换函数用法详解

Python基础学习之时间转换函数用法详解

本文实例讲述了Python基础学习之时间转换函数用法。分享给大家供大家参考,具体如下: 前言 python的时间格式分为多种,几种格式之间的转换方法时常是我们遇到的而且是经常忘记的点,p...