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对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...

  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. 【Foreign】光 [莫比乌斯反演]

    光 Time Limit: 10 Sec  Memory Limit: 128 MB Description 天猫有一个长方形盒子,长宽分别为A,B. 这个长方形盒子的内壁全部是镜面. 天猫在这个盒子 ...

  2. Hibernate数据连接不能正常释放的原因,以及在监听中获取apolicationContext上下文

    Hibernate数据库连接不能正常释放: https://blog.csdn.net/u011644423/article/details/44267301 监听中获取applicationCont ...

  3. 微信小程序提示框

    一.wx.showToast 如上图所示,showToast会显示一个弹窗,在指定的时间之后消失.中间的图标默认只有加载中和成功两种,也可以用image参数自定义图标 wx.showToast({ t ...

  4. 21、python操作redis的模块?

    什么是redis? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...

  5. Ubuntu安装pip

    首先打开终端 在终端输入:sudo apt-get install python-pip python-dev build-essential [+] 如果需要在Python3下安装pip,那么在py ...

  6. Vue组件-使用插槽分发内容

    在使用组件时,我们常常要像这样组合它们: <app> <app-header></app-header> <app-footer></app-fo ...

  7. Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】

    转自:http://www.th7.cn/system/lin/201606/167750.shtml 我们都知道在用户空间动态申请内存用的函数是 malloc(),这个函数在各种操作系统上的使用是一 ...

  8. Linux内核基础--事件通知链(notifier chain)【转】

    转自:http://blog.csdn.net/wuhzossibility/article/details/8079025 内核通知链 1.1. 概述 Linux内核中各个子系统相互依赖,当其中某个 ...

  9. linux-open-source-development-tools【重点】

    https://www.pluralsight.com/blog/software-development/linux-open-source-development-tools https://ww ...

  10. python设计模式之单例模式(一)

    单例设计模式的概念: 单例设计模式即确保类有且只有一个特定类型的对象,并提供全局访问点.一般我们操作数据库的时候为了避免统一资源产生互相冲突,创建单例模式可以维护数据的唯一性. 单例模式的特性: 确保 ...