做网络编程的时候,经常需要把接收到的数据用16进制的方式打印出来,方便查看。今天发现在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

encode和decode的妙用相关推荐

  1. 2019年2月26日 Unique Email Addresses、To Lower Case、Encode and Decode TinyURL

    今天开始加快速度,趁着还有空多刷几题,语言换成python提高速度了. 1. Unique Email Addresses 弱题,注意@符号前后的处理方式不同 class Solution(objec ...

  2. python3 的encode 和 decode

    str ----> bytes: encode 编码 bytes----> str: decode 解码 str.encode() bytes.decode() >>> ...

  3. decode函数python在哪里_Python基础知识——encode和decode函数

    以前我们介绍过,Python2.x中默认的编码的基础类型是unicode编码的类型,在Python3.x才转化为基于unicode的字符串. 那么我们在Python2.x的学习中就会遇到各种各样的编码 ...

  4. pythonunicode和str_python的str,unicode对象的encode和decode方法

    python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...

  5. python中的encode()和decode()函数

    对于很多人来说,python的中字符转码是一件很头疼的事情,本来期望结果输出的是中文,结果来一段像这样\xe4\xbd\xa0\xe5\xa5\xbd像是乱码的字符串. 由于学python没多久,昨天 ...

  6. Python zipfile 文件名称编码 file_name.encode(‘cp437‘).decode(‘gbk‘)

    主要是处理压缩包里的中文,不出现乱码 with zipfile.ZipFile('love.zip', 'r') as zipobj: #读取压缩包for file_name in zipobj.na ...

  7. python str字符串和byte字节流的区别 encode、decode方法的区别

    Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str和b ...

  8. pythonencode_python的encode和decode误读总结

    python的encode和decode误读总结 最近在学Python,对编码有个误解的地方 下面是错误的理解: encode():编码,将对象的编码转换为指定编码格式,按照字面理解,一直以为是其他编 ...

  9. Python字符串的encode与decode研究心得乱码问题解决方法

    Python字符串的encode与decode研究心得乱码问题解决方法 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成"\xe4\xb8\xad\xe6\x96 ...

最新文章

  1. CENTOS 7 nginx-1.6.2 下载 解压 安装
  2. 几张图看懂列式存储(转)
  3. python 第三方库 工具
  4. mysql 字符串索引 优化_MySQL性能优化之索引调优实战
  5. mysql使用手册_mysql使用手册
  6. ElasticSearch D3
  7. LiveCDLiveUSB制作
  8. 内蒙古师范大学计算机与科学,2021年内蒙古师范大学计算机科学与技术(077500)硕士研究生招生信息_考研招生计划和招生人数 - 学途吧...
  9. 区块链需要学习哪些东西_区块链主要学习哪些知识?
  10. [文摘20070930]每个人都需要掌握的18个人情世故
  11. MySQL事务(脏读、不可重复读、幻读)
  12. C语言基础5-预编译部分知识(#include+.h文件和.c文件的区别
  13. sqlserver按'一二三四五'排序(hibernate对中文数字进行排序)
  14. 安捷伦频谱仪的使用方法图解_频谱仪的简单操作使用方法lbrack;1rsqb;
  15. Java二叉树的最大深度
  16. linux终端 快捷键
  17. 一起学libcef--搭建自己的libcef运行环境(Win32程序,错误C2220解决方案)
  18. android麦克风被禁用怎么办,为什么微信麦克风被禁用?如何开启?
  19. 扬帆际海—移动端流量对跨境电商有多重要?
  20. 西门子plc与oracle报文,西门子PLC以太网 通讯协议 解析

热门文章

  1. 使用 MP4box 给MP4视频嵌入字幕(特别支持iphone/ipad/ipod)
  2. 第一篇论文的写作流程
  3. 软件测试qa的工作职责,现场QA检查员岗位职责
  4. python剪贴板操作_python 操作剪切板
  5. 单片机毕业设计 STM32自动泊车系统 - 智能小车 自动停车
  6. C语言:定义一个数组,5个元素,从xiao到da排序
  7. 修复VBS病毒感染的html文件
  8. itertools模块中的product方法
  9. dc-dc开关电源的纹波测试
  10. 关于SAN和NAS的区别!!!!