Python chr() ord() unichr()

 

chr()函数用一个范围在range(256)内的(就是0~255)整数作参数,返回一个对应的字符.

unichr()跟它一样,只不过返回的是Unicode字符,这个从Python 2.0才加入的unichr()的参数范围依赖于你的Python是如何被编译的。如果是配置为USC2的Unicode,那么它的允许范围就是range(65536)或0x0000-0xFFFF;如果配置为UCS4,那么这个值应该是range(1114112)或0x000000-0x110000。如果提供的参数不在允许的范围内,则会报一个ValueError的异常

ord()函数是chr()函数(对于8位的ASCII字符串)或unichr()函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数,返回对应的ASCII数值,或者Unicode数值,如果所给的Unicode字符超出了你的Python定义范围,则会引发一个TypeError的异常。

  • ord是unicode ordinal的缩写,即编号
  • chr是character的缩写,即字符
  • ord和chr是互相对应转换的.
  • 但是由于chr局限于ascii,长度只有256,于是又多了个unichr.

 

>>> chr(69)
'E'
>>> ord("A")
65
>>> unichr(500)
u'\u01f4'
>>> print unichr(500)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'gbk' codec can't encode character u'\u01f4' in position 0: illegal multibyte sequence

#当所有的编码格式都无法通过时,可以encode成utf8输出
>>> print unichr(500).encode("utf-8")
谴

>>> a=u''
>>> ord(a)
35892
>>> a=''
>>> ord(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
>>> chr(35892)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(256)
>>> unichr(35892)
u'\u8c34'
>>> print unichr(35892).encode("utf-8")
璋
>>> print unichr(35892).encode("gbk")
谴

 

posted on 2016-12-25 22:09  大大的橙子  阅读(458)  评论(0编辑  收藏  举报

导航