数字转字符 - chr()
字符转数字 - ord()
于是就出现一个有意思的事情,chr()可以将数字转化为字符,而且根据python3的IDLE提示,chr()的范围为0<=i<=0x10ffff,如果我们遍历这之间的数值会有什么现象呢,如下:
1 2 3 4 5 6 7 8 9 10 |
for i in range(0,0x10ffff): print(format("%6X: %s") % (i, chr(i)) ) ----------------------------------------------------------------------- FFFF: <br>Traceback (most recent call last): File "C:\Users\jma\Desktop\chr.py", line 9, in <module> print(format("%10X: %s") % (i, chr(i)) ) UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 12-12: Non-BMP character not supported in Tk |
在超过65535的时候,系统就提示错误了,可是同样的程序在cmd里面却可以正常运行,虽然chr()打印出来的结果都是方框。原来是chr()字符已经超出了IDLE可接受的BMP( Basic Multilingual Plane)范围,这种情况要额外处理下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# -*- coding:utf-8 -*- import sys non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd) for i in range(65534,0x10ffff): if(i<65535): print(format("%6X: %s") % (i, chr(i)) ) else: print(format("%10X: %s") % (i, chr(i).translate(non_bmp_map)) ) |
如此便能在IDLE中正常执行下去,原文可参看 Stack Overflow