Python, Unicode, and the Windows console

前端 未结 13 2163
佛祖请我去吃肉
佛祖请我去吃肉 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:02

    James Sulak asked,

    Is there any way I can make Python automatically print a ? instead of failing in this situation?

    Other solutions recommend we attempt to modify the Windows environment or replace Python's print() function. The answer below comes closer to fulfilling Sulak's request.

    Under Windows 7, Python 3.5 can be made to print Unicode without throwing a UnicodeEncodeError as follows:

        In place of:    print(text)
        substitute:     print(str(text).encode('utf-8'))

    Instead of throwing an exception, Python now displays unprintable Unicode characters as \xNN hex codes, e.g.:

      Halmalo n\xe2\x80\x99\xc3\xa9tait plus qu\xe2\x80\x99un point noir

    Instead of

      Halmalo n’était plus qu’un point noir

    Granted, the latter is preferable ceteris paribus, but otherwise the former is completely accurate for diagnostic messages. Because it displays Unicode as literal byte values the former may also assist in diagnosing encode/decode problems.

    Note: The str() call above is needed because otherwise encode() causes Python to reject a Unicode character as a tuple of numbers.

提交回复
热议问题