python encode和decode的妙用

所属分类: 脚本专栏 / python 阅读数: 1974
收藏 0 赞 0 分享

>>> "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中的对象与类型

这篇文章主要介绍了深入源码解析Python中的对象与类型,涉及到对象的引用计数方法和类型的定义等深层次内容,需要的朋友可以参考下
收藏 0 赞 0 分享

一篇文章入门Python生态系统(Python新手入门指导)

原文写于2011年末,虽然文中关于Python 3的一些说法可以说已经不成立了,但是作为一篇面向从其他语言转型到Python的程序员来说,本文对Python的生态系统还是做了较为全面的介绍
收藏 0 赞 0 分享

Python实时获取cmd的输出

本文给大家分享python实时获取cmd的输出,对python实时获取输出相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

分享Python字符串关键点

字符串是 Python 中最常用的数据类型。我们可以使用引号来创建字符串,通过本篇文章给大家分享python字符串关键点相关资料,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Python中内置数据类型list,tuple,dict,set的区别和用法

这篇文章主要给大家介绍了Python中内置数据类型list,tuple,dict,set的区别和用法,都是非常基础的知识,十分的细致全面,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

Python学习笔记整理3之输入输出、python eval函数

这篇文章主要介绍了Python学习笔记整理3之输入输出、python eval函数的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

十个Python程序员易犯的错误

不管是在学习还是工作过程中,人都会犯错。虽然Python的语法简单、灵活,但也一样存在一些不小的坑,一不小心,初学者和资深Python程序员都有可能会栽跟头。本文为大家分享了10大常见错误,需要的朋友可以参考下
收藏 0 赞 0 分享

使用Python编写简单的端口扫描器的实例分享

这篇文章主要介绍了使用Python编写简单的端口扫描器的实例分享,文中分别介绍了单线程和多线程的实现方式,需要的朋友可以参考下
收藏 0 赞 0 分享

用ReactJS和Python的Flask框架编写留言板的代码示例

这篇文章主要介绍了用ReactJS和Python的Flask框架编写留言板的代码示例,其他的话用到了MongoDB这个方便使用JavaScript来操作的数据库,需要的朋友可以参考下
收藏 0 赞 0 分享

在DigitalOcean的服务器上部署flaskblog应用

这篇文章主要介绍了在DigitalOcean的服务器上部署flaskblog的方法,flaskblog是用Python的Flask开发的一个博客程序,而DigitalOcean则是大受欢迎的SSD主机提供商,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多