Python, Unicode, and the Windows console

前端 未结 13 2114
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:38

When I try to print a Unicode string in a Windows console, I get a UnicodeEncodeError: \'charmap\' codec can\'t encode character .... error. I assume this is b

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 05:03

    The cause of your problem is NOT the Win console not willing to accept Unicode (as it does this since I guess Win2k by default). It is the default system encoding. Try this code and see what it gives you:

    import sys
    sys.getdefaultencoding()
    

    if it says ascii, there's your cause ;-) You have to create a file called sitecustomize.py and put it under python path (I put it under /usr/lib/python2.5/site-packages, but that is differen on Win - it is c:\python\lib\site-packages or something), with the following contents:

    import sys
    sys.setdefaultencoding('utf-8')
    

    and perhaps you might want to specify the encoding in your files as well:

    # -*- coding: UTF-8 -*-
    import sys,time
    

    Edit: more info can be found in excellent the Dive into Python book

提交回复
热议问题