python的str,unicode对象的encode和decode方法
python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byte[]。
而python中的unicode对象应该才是等同于java中的String对象,或本质上是java的char[]。
对于

  1. s="你好"
  2. u=u"你好"
s="你好"
u=u"你好" 

1. s.decode方法和u.encode方法是最常用的,
简单说来就是,python内部表示字符串用unicode(其实python内部的表示和真实的unicode是有点差别的,对我们几乎透明,可不考虑),和人交互的时候用str对象。
s.decode -------->将s解码成unicode,参数指定的是s本来的编码方式。这个和unicode(s,encodename)是一样的。
u.encode -------->将unicode编码成str对象,参数指定使用的编码方式。
助记:decode to unicode from parameter
encode to parameter from unicode
只有decode方法和unicode构造函数可以得到unicode对象。
上述最常见的用途是比如这样的场景,我们在python源文件中指定使用编码cp936,
# coding=cp936或#-*- coding:cp936 -*-或#coding:cp936的方式(不写默认是ascii编码)
这样在源文件中的str对象就是cp936编码的,我们要把这个字符串传给一个需要保存成其他编码的地方(比如xml的utf-8,excel需要的utf-16)
通常这么写:
strobj.decode("cp936").encode("utf-16")

You
typically encode a unicode string whenever you need to use it for IO,
for instance transfer it over the network, or save it to a disk file.
To convert a string of bytes to a unicode string is known as decoding. Use unicode('...', encoding) or '...'.decode(encoding).
You typically decode a string of bytes whenever you receive string data from the network or from a disk file.
2.
第一条已经写了不少,因为是最常用到的,基本不用怎么解释。我重点想说的是这第二条。
似乎有了unicode对象的encode方法和str的decode方法就足够了。奇怪的是,unicode也有decode,而str也有
encode,到底这两个是干什么的。
用处1
str本身已经是编码过的了,如果再encode很难想到有什么用(通常会出错的)
先解释下这个
str.encode(e) is the same as unicode(str).encode(e).
This is useful since code that expects Unicode strings should also work when it is passed
ASCII-encoded 8-bit strings(from Guido van Rossum)
python之父的这段话大概意思是说encode方法本来是被unicode调的,但如果不小心被作为str对象的方法调,并且这个str对象正好
是ascii编码的(ascii这一段和unicode是一样的),也应该让他成功。这就是str.encode方法的一个用处(我觉得这个基本等于没用)
类似地,把光用ascii组成的unicode再decode一回是一样的道理,因为好像几乎任何编码里ascii都原样没变。因此这样的操作等于没做。
u"abc".decode("gb2312")和u"abc"是相等的。

用处2
非字符的编码集non-character-encoding-codecs,这些只在python中定义,离开python就没意义(这个来自python的官方文档)
并且也不是人类用的语言,呵呵。
比如

  1. '\n'.encode('hex')=='0a'
  2. u'\n'.encode('hex')=='0a'
  3. '0a'.decode('hex')=='\n'
  4. u'0a'.decode('hex')=='\n'
'\n'.encode('hex')=='0a'
u'\n'.encode('hex')=='0a'
'0a'.decode('hex')=='\n'
u'0a'.decode('hex')=='\n'

可见名为hex的编码可以讲字符表示(当然了,必须是ascii内的)和十六进制表示之间转换
另外还有很多好玩的,比如:base64通俗的讲是号称防君子不防小人的给邮件的编码,gzip大概是指压缩吧(这是我猜的),rot13回转13等,不知者google之
关于这些,官方有个详细的表格,在http://docs.python.org/library/codecs.html中的Standard Encodings一节中,前一个表格是基于字符的编码,第二个表格
就是这里的非字符的编码。关于这些特殊编码,官方一句说明:
For the codecs listed below, the result in the “encoding” direction is always a byte string.
The result of the “decoding” direction is listed as operand type in the table.
encode的结果一定是一个byte的str,而decode的结果在表中operand一列。

参考
Converting Between Unicode and Plain Strings 在Unicode和普通字符串之间转换
http://wiki.woodpecker.org.cn/moin/PyCkBk-3-18
what’s the difference between encode/decode? (python 2.x)
http://stackoverflow.com/questions/447107/whats-the-difference-between-encode-decode-python-2-x
http://docs.python.org/library/codecs.html

编码声明的作用
请参考http://www.python.org/dev/peps/pep-0263/
声明源文件中将出现非ascii编码;
在高级的IDE中,IDE会将你的文件格式保存成你指定编码格式。
决定源码中类似于u'哈'这类声明的将'哈'解码成unicode所用的编码格式,也是一个比较容易让人迷惑的地方。
(java不需要声明的原因在于:java中默认是本地编码而py中默认是ascii,搞得python更易出错,
并且,java编译的时候还有个指定编码的参数encoding)

文件的编码格式决定了在该源文件中声明的字符串的编码格式,例如:

  1. str = '哈哈'
  2. print repr(str)
str = '哈哈'
print repr(str) 

a.如果文件格式为utf-8,则str的值为:'\xe5\x93\x88\xe5\x93\x88'(哈哈的utf-8编码)
b.如果文件格式为gbk,则str的值为:'\xb9\xfe\xb9\xfe'(哈哈的gbk编码)

我的理解:文件编码格式保存后没有地方指明,只有靠聪明或笨的编辑器,编译器去猜。而声名就更精确一些。
让两者一致了总不会错。

其实好多其他语言或应用中也是类似的decode和encode概念,比如在java中String的涉及的编码转换及jdk中的工具native2ascii,
好像javascript也有这个,记不清楚了。

python的str,unicode对象的encode和decode方法的更多相关文章

  1. python的str,unicode对象的encode和decode方法(转)

    python的str,unicode对象的encode和decode方法(转) python的str,unicode对象的encode和decode方法 python中的str对象其实就是" ...

  2. python的str,unicode对象的encode和decode方法, Python中字符编码的总结和对比bytes和str

    python_2.x_unicode_to_str.py a = u"中文字符"; a.encode("GBK"); #打印: '\xd6\xd0\xce\xc ...

  3. 【python】python新手必碰到的问题---encode与decode,中文乱码[转]

    转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...

  4. [转]python新手必碰到的问题---encode与decode,中文乱码--转载

    edu.codepub.com/2009/1029/17037.php 这个问题在python3.0里已经解决了. 这有篇很好的文章,可以明白这个问题: 为什么会报错“UnicodeEncodeErr ...

  5. python学习笔记(9)--Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法

    Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法 这篇文章主要介绍了Python UnicodeEncodeErro ...

  6. Python字符串的编码与解码(encode与decode)

    首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unico ...

  7. python中自定义类对象json字符串化的方法

    1. 用 json 或者simplejson 就可以 2.定义转换函数: def convert_to_builtin_type(obj): print 'default(', repr(obj), ...

  8. [转]Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法

    使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character ' ...

  9. python day- 6 is 和 ==的区别 encode 和 decode

    1.is 和  == 的区别. == 是由来判断左右两边的内容是否相等. is 是用来判断内存地址是否相同. 引进 id (   )函数 小数据池: 对于字符串 ,数字 ,bool 值进行 id()计 ...

随机推荐

  1. HTML5的绘图的支持

    一.简单介绍canvas元素 <canvas.../>是HTML5新增的一个元素,该元素用于绘制图形.实际上<canvas../>只是相当于一张画布. 它除了可以指定通用属性外 ...

  2. 桦仔 笔记4-徐 模仿灾难发生时还原adventurework数据库 示例 stopat

    1 --模仿灾难发生时还原adventurework数据库 示例 stopat 2 3 BACKUP DATABASE AdventureWorks 4 TO DISK= 'D:\MSSQL\Data ...

  3. Oracle 客户端配置笔记

    1.右击桌面的我的电脑 -> 高级 -> 环境变量,新建 1) 变量名:ORACLE_HOME 变量值:D:\app\instantclient_11_2 2) 变量名:TNS_ADMIN ...

  4. Struts1、Struts2的线程安全问题

    Struts 1.x和Struts 2的Action是不是线程安全的? Struts 1.x在第一次请求某个Action时,会创建这个Action实例.但之后再请求该Action实例时,就用之前创建好 ...

  5. iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点

    1.首先建立这个三个参数 @property (nonatomic,strong)CBCentralManager * manager; @property (nonatomic,strong)CBP ...

  6. Unity StrangeIoc框架 (二)

    MVCSContex :the big picture 1.应用程序的入口是一个类成为ContextView,这是一个Monobehavior实例化MVCSContext 2.用MVCSContext ...

  7. math。h中的log函数的应用

    以10为底的log函数: 形式为 double  log10(double  x) 以e为底的log函数(即 ln)double log (double x) 如何表达log 以a为底b的对数: 用换 ...

  8. Performance tool httperf

    httperf: A relatively well-known open source utility developed by HP, for Linux operating systems on ...

  9. mina、netty消息边界问题(采用换行符)

    在TCP连接开始到结束连接,之间可能会多次传输数据,也就是服务器和客户端之间可能会在连接过程中互相传输多条消息.理想状况是一方每发送一条消息,另一方就立即接收到一条,也就是一次write对应一次rea ...

  10. 一年开发ASP.NET MVC4项目经验总结

    一年开发ASP.NET MVC4项目所用所学技术经验总结 阅读目录 文章背景 前端所用技术摘要 后端所用技术摘要 1. 文章背景 本人2014年从Java转行到C#从事BS项目的开发,刚开始接触的是A ...