26 Python 2.x 字符编码终极指南
2017-01-06 11:40
120 查看
强烈推荐,讲的很透彻…
1. http://python.jobbole.com/87145/
2. https://www.azavea.com/blog/2014/03/24/solving-unicode-problems-in-python-2-7/
**以下是最重要的:
1. str可以看作是unicode字符串经过某种编码后的字节组成的数组;
2. unicode是真正意义上的字符串;
3. 通过 encode 可以将unicode类型编码为str类型;
4. 通过 decode 可以将str类型解码为unicode类型;
5. python 会隐式地进行编码、解码,默认采用 ascii;
6. 所有的编码、解码错误都是由于所选的编码、解码方式无法表示某些字符造成的;**
转载请标明出处,原文地址(http://blog.csdn.net/lis_12/article/details/).
如果觉得本文对您有帮助,请点击‘顶’支持一下,您的支持是我写作最大的动力,谢谢。
1. http://python.jobbole.com/87145/
2. https://www.azavea.com/blog/2014/03/24/solving-unicode-problems-in-python-2-7/
**以下是最重要的:
1. str可以看作是unicode字符串经过某种编码后的字节组成的数组;
2. unicode是真正意义上的字符串;
3. 通过 encode 可以将unicode类型编码为str类型;
4. 通过 decode 可以将str类型解码为unicode类型;
5. python 会隐式地进行编码、解码,默认采用 ascii;
6. 所有的编码、解码错误都是由于所选的编码、解码方式无法表示某些字符造成的;**
# -*- coding: utf-8 -*-的作用见https://www.python.org/dev/peps/pep-0263/
#!/usr/bin/python # -*- coding: utf-8 -*- 用来指定文件编码为utf-8的,如果不指定将会使用系统默认的编码方式 '''windows7 平台下测试''' '''查看系统的隐式编码方式''' import locale print locale.getdefaultlocale() a = 'Test 测试' b = u'Test 测试' '''str是字节串,隐含了某种编码方式的字节码,由unicode经某种编码方式编码而来''' print a,type(a) #Test 娴嬭瘯 <type 'str'> '''这个才是字符串''' print b,type(b) #Test 测试 <type 'unicode'> ''' 我们可以将<type 'unicode'>看作是一系列字符组成的数组,数组的每一项是一个代码点,用来表示相应位置的字符. 所以对于unicode来说,其长度等于它包含的字符个数.(unicode为万国码,也可以理解为中间码) 对str来说,其长度等于字节个数 ''' print repr(a) #'Test \xe6\xb5\x8b\xe8\xaf\x95' print repr(b) #u'Test \u6d4b\u8bd5',\u6d4b表示'测',\u8bd5表示'试' print len(a) #11 print len(b) #7 '''可以将<type 'str'>看作是unicode字符串经过某种编码后的字节组成的数组. 数组的每一项是一个字节。所以对于 str 字符串来说,其长度等于编码后字节的长度。''' print a #Test 娴嬭瘯 print bytearray(b,'utf-8') #Test 娴嬭瘯 print bytearray(b,'gbk') #Test 测试 print bytearray(b,'cp936') #Test 测试 '''解码,因为str类型是隐含有某种编码方式的字节码,此脚本指定为utf-8,所以用utf-8解码''' t = a.decode('utf-8') print type(t),t,len(t) #<type 'unicode'> Test 测试 7 '''编码''' g = t.encode('utf-8') print type(g),g,len(g) #<type 'str'> Test 娴嬭瘯 11 g = t.encode('gbk') print type(g),g,len(g) #<type 'str'> Test 测试 9 g = t.encode('cp936') print type(g),g,len(g) #<type 'str'> Test 测试 9 '''用ascii编码/解码带有中文的unicode字符串时,会发生UnicodeEncodeError/UnicodeDecodeError, 因为ascii只包含127个字符,无法表示中文.''' #g = t.encode('ascii') #UnicodeEncodeError: 'ascii' codec can't encode characters in position 5-6: ordinal not in range(128) #print type(g),g,len(g) ''' 隐藏的解码 因为str类型是隐含有某种编码方式的字节码,所以python内部将其解码为unicode后,再和unicode类型进行 + 操作,最后返回的结果也是unicode类型。 解码过程是在幕后悄悄发生的,默认采用ascii来进行解码,Python 之所以采用 ascii,是因为 ascii 是最早的编码方式,是许多编码方式的子集。 ''' try: t = u'hello' + 'world' #helloworld <type 'unicode'> print t,type(t) print u'hello' + '世界' #等价于u'hello' + '世界'.decode('ascii') ,用ascii解码中文会发生错误 except Exception,e: print e ''' 隐藏的编码 Python不只偷偷地用ascii来解码str类型的字节串,有时还会偷偷用ascii来编码unicode类型。如果函数或类等对象接收的是 str 类型的字符串,但传进去的是unicode,python2 就会使用 ascii 将其编码成str类型再做运算。 如果在终端进行输出,则不会抛出异常.因为Python会使用控制台的默认编码,而不是ascii ''' ''' 总结下本文的内容: str可以看作是unicode字符串经过某种编码后的字节组成的数组 unicode是真正意义上的字符串 通过 encode 可以将unicode类型编码为str类型 通过 decode 可以将str类型解码为unicode类型 python 会隐式地进行编码、解码,默认采用 ascii 所有的编码、解码错误都是由于所选的编码、解码方式无法表示某些字符造成的 如果你明白了上面每句话的含义,那么应该能解决大部分编、解码引起的问题了。 '''
转载请标明出处,原文地址(http://blog.csdn.net/lis_12/article/details/).
如果觉得本文对您有帮助,请点击‘顶’支持一下,您的支持是我写作最大的动力,谢谢。
相关文章推荐
- opencv:fatal error LNK1104: 无法打开文件“python26_d.lib” 问题解决
- Python天天美味(26) - __getattr__与__setattr__
- python26实例[subst然后删除文件夹]
- python实例26[查询修改文件的属性]
- python类库26[sqlite]
- python26高级[读写Excel]
- python技巧26[str+unicode+codecs]
- python类库26[web2py与MVC]
- python类库26[web2py之基本概念]
- FreeBSD python25 move to python26 as default version
- python实例26[验证用户是否存在于LDAP Server]
- python类库26[读写Excel]
- python26实例[根据正则表达式来清理文件夹]
- python技巧26[python的egg包的安装和制作]
- D:/Python26/lib/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import Immut
- python26 中文格式输出 支持
- python类库26[读写mysql]
- python类库26[PySide之helloworld]
- python技巧26[python的egg包的安装和制作]
- Learn Python The Hard Way学习(26) - 恭喜,可以进行期中考试了!