python3输出unicode

来源:互联网 发布:java软件eclipse中文版 编辑:程序博客网 时间:2024/06/14 16:44

采用ord函数即可

In Python 2.x

Python代码  收藏代码
  1. D:\Python27>python  
  2.   
  3. >>> chr(65)  
  4. 'A'  
  5.   
  6. >>> ord('A')  
  7. 65  
  8.   
  9. >>> unichr(20013)  
  10. u'\u4e2d'  
  11.   
  12. >>> ord(u'\u4e2d')  
  13. 20013  
  14.   
  15. >>> ord('中')  
  16. Traceback (most recent call last):  
  17.   File "<stdin>", line 1in <module>  
  18. TypeError: ord() expected a character, but string of length 2 found  
  19.   
  20. >>> chr(20013)  
  21. Traceback (most recent call last):  
  22.   File "<stdin>", line 1in <module>  
  23. ValueError: chr() arg not in range(256)  

 In Python  3.x

Python代码  收藏代码
  1. D:\CODE\PYTHON\OPEN_JUDGE>python  
  2. Python 3.2.5 (default, May 15 201323:06:03) [MSC v.1500 32 bit (Intel)] on win32  
  3. Type "help""copyright""credits" or "license" for more information.  
  4.   
  5. >>> chr(65)  
  6. 'A'  
  7.   
  8. >>> ord('A')  
  9. 65  
  10.   
  11. >>> chr(20013)  
  12. '中'  
  13.   
  14. >>> ord('中')  
  15. 20013  
  16.   
  17. >>> ord('\u4e2d')  
  18. 20013  

 


0 0