理解python中的bytestring和unicode
2008-12-11 13:32
295 查看
编码的概念
首先让我们来了解一些有关编码的背景知识。在计算机世界中的,一个字节(byte)通常有8位(bit),能表示256个字符(character)。不过当今世界不同的语言中包含的文字成千上万,一个字节所能表示的字符个数远远不能满足实际的需要,所以我们要用更多的字节来表示更多的字符。用字节来表示字符的过程称之为编码。ASCII编码共有127字符,所以每个字符可以用一个字节表示。覆盖众多西欧语言的ISO-8859(Latin)系列编码是 ASCII编码的超集,每个字符也使用一个字节来表示,最多可以表示256个字符。在我们的国标码GB2312中,每个字符用两个字节表示。 Windows NT的内部使用UCS-2编码,也是双字节编码。Linux平台上流行的UTF-8编码是可变长编码,单字节兼容ASCII编码,双字节覆盖西欧语言,三字节用来编码中、日、韩(CJK)文字。请看下面的图示,横列是字符串,竖列是相应编码后的字节序列,竖列框中用十六进制表示字节的值:
+-+-+-+-+-+-+ +--+--+ +--+--+ ASCII |p|y|t|h|o|n| GB2312 |巨|蟒| UTF-8 |巨|蟒| +-+-+-+-+-+-+ +--+--+ +--|--+ | | | | | | | | | | | | | +----+<---+ | | | | | | +----+<--+ | | +----+<--+ | | |0x70| | | | | | | |0xbe| | | |0xe5| | | +----+<-----+ | | | | | +----+ | | +----+ | | |0x79| | | | | | |0xde| | | |0xb7| | | +----+<-------+ | | | | +----+<-----+ | +----+ | | |0x74| | | | | |0xf2| | |0xa8| | | +----+<---------+ | | | +----+ | +----+<-----+ | |0x68| | | | |0xfe| | |0xe8| | +----+<-----------+ | | +----+<--------+ +----+ | |ox6f| | | |0x9f| | +----+<-------------+ | +----+ | |0x6e| | |0x92| | +----+<---------------+ +----+<--------+
编码方面就简单介绍到这了,下面进入python部分。
python中的bytestring
在python的交互模式下尝试输入下面的语句:>>> '\x41' 'A' >>> '\172' 'z'
如上所示,在python字符串(这里的字符串都指单个引号包围的字符串)中,可以使用'\'来进行转义。'\x41'使用两位十六进制数来表示一个字节的值,在ASCII中代表'A'。'\172'使用三位八进制数来表示一个字节的值,在ASCII中代表'z'。
让我们来输入一些中文(windows下):
>>> bytestr = 'python巨蟒' >>> bytestr 'python\xbe\xde\xf2\xfe' >>> len(bytestr) 10
中文环境下,cmd的代码页是cp936(即是GBK编码,GB2312的扩展编码),每个汉字占两个字节。因此'python巨蟒'共10个字节,可以看出python中的字符串其实是字节串(byte string)。
python中的unicode
python 中除了byte string,还有unicode string。因为unicode大到足以容纳我们用到的所有字符,所以可以把unicode看成对字符的一种抽象表示。使用unicode的明显好处是可以处理更大的字符集,所以常常要把byte string转换成unicode string,这个过程称为解码。现在让我们来对byte string进行解码,将其转换成unicode string
>>> bytestr = 'python巨蟒' >>> unicodestr = bytestr.decode('cp936') >>> unicodestr u'python\u5de8\u87d2' >>> len(unicodestr) 8
可以看到unicodestr的长度是8,和'python巨蟒'的字符个数一样。
也可以照下面这样做:
>>> bytestr = 'python巨蟒' >>> unicodestr = unicode(bytestr, 'cp936')
之后,可以对unicode string进行不同格式的编码,转化为相应的byte string
>>> unicodestr.encode('cp936') 'python\xbe\xde\xf2\xfe' >>> unicodestr.encode('utf-8') 'python\xe5\xb7\xa8\xe8\x9f\x92'
因为cmd的代码页是cp936,所以把unicodestr按utf-8编码后,会打印出的乱码字符
>>> utf8bytestr = unicodestr.encode('utf-8') >>> utf8bytestr 'python\xe5\xb7\xa8\xe8\x9f\x92' >>> print utf8bytestr python宸ㄨ煉
python脚本中的编码指定
如果是在python的脚本文件中,可以在首行或者紧跟着#!行注明编码# -*- encoding: utf-8 -*-
下面是一段脚本
# -*- encoding: utf-8 -*- unicodestr = unicode('python巨蟒', 'utf-8') print unicodestr unicodestr = unicode('python巨蟒', 'cp936') print unicodestr unicodestr = u'python巨蟒' print unicodestr
输出结果如下
python巨蟒 python宸ㄨ煉 python巨蟒
一个小问题
可以看到,u'python巨蟒'中的字符串被自动解码,转化为unicode,这是python中另一种定义unicode字符串的方式。不过这种方式在GUI shell下会有一些问题。比如在IDLE中输入u'巨蟒'结果如下:>>> u'巨蟒' u'\xbe\xde\xf2\xfe'
而在python的交互模式下就不会有问题:
>>> u'巨蟒' u'\u5de8\u87d2'
GUI shell其实是使用code标准模块来进行read-eval-print loop的。
另外通过调用命令行"python -c command"来执行代码结果也是如此。在cmd中输入
python -c "for x in u'巨蟒': print hex(ord(x))"
结果是:
0xbe 0xde 0xf2 0xfe
相关文章推荐
- python decode encode unicode 中文乱码 的理解
- 有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
- 有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
- 深入理解Python的字符编码utf-8 & unicode
- 如何理解python3的unicode,以及全角半角转换
- 如何理解 python UnicodeEncodeError :python 的 string 和 unicode
- 有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
- 有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
- 对于python的egg安装的理解
- python 中 深拷贝和浅拷贝的理解
- <python 深入理解>变量交换x,y=y,x实现机制--元组
- Python - 深刻理解Python中的元类(metaclass)
- 解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
- python写入文件编码问题UnicodeEncodeError
- 理解python并发编程-进程篇
- 深刻理解Python中的元类(metaclass)
- python 显示unicode字符问题
- python编码问题 decode('unicode-escape')
- 对字符编码与Unicode,ISO 10646,UCS,UTF8,UTF16,GBK,GB2312的理解
- python简易爬虫,帮助理解re模块