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. C#经典之Application.DoEvents()的使用

    最近做了一个文件上传的模块,因为牵扯到电脑文件的扫描,想做一个实时显示当前扫面文件的功能,就类似于360文件扫描时的效果,本来打算用多线程来实现,但是方法太多没有实现,后来在程序中进行控制,由于文件太 ...

  2. class A<T> where T:class 这个泛型类中的Where T:class什么意思

    这是类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct                               T必须是一个结构类型 where T : cla ...

  3. shell检测interface是否已分配ip,qt调用shell脚本

    #include <QCoreApplication>#include <QDebug>#include <QTextStream>#include <QDi ...

  4. Centos 7 安装Mono和Jexus 默认目录安装 (一)

    一.准备环境 yum -y install gcc gcc-c++ bison pkgconfig glib2-devel gettext make libpng-devel libjpeg-deve ...

  5. 50句高级SQL语句

    一个题目涉及到的50个Sql语句 --(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID) ------------------ ...

  6. android双击返回键退出程序

    今天给大家简单说一下,android双击返回键退出程序. @Override public boolean onKeyDown(int keyCode, KeyEvent event) {      ...

  7. win7本地搭建git ssh服务器

    本来是想在gogs上用ssh的,结果弄了好几次还没整明白,希望等他们的更新内置吧. 但是,意外收获,还是成功搭建了本地ssh服务器,只是没有和gogs成功关联. 简要记录一下: 主要软件: msysg ...

  8. ZOJ 3741 Eternal Reality

    Eternal Reality Time Limit: 2 Seconds                                      Memory Limit: 65536 KB In ...

  9. hdu 2471 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=(  dp[n-1][m],dp[n][m-1],d[i][k ...

  10. 4_Is Prime

    4 // // ViewController.swift // Is Prime // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. All ...