How to print utf-8 to console with Python 3.4 (Windows 8)?

后端 未结 5 1537
再見小時候
再見小時候 2020-12-29 03:58

I\'ve never fully wrapped my head around encoding and decoding unicode to other formats (utf-8, utf-16, ascii, etc.) but I\'ve reached a wall thatis both confusing and frust

5条回答
  •  [愿得一人]
    2020-12-29 04:30

    By default, the console in Microsoft Windows only displays 256 characters (cp437, of "Code page 437", the original IBM-PC 1981 extended ASCII character set) as you say in comments.

    and in other side the PYTHONIOENCODING is set to UTF-8 by default. so i think when you want to print unicode in windows you have to align sys.stdout.encoding and PYTHONIOENCODING with together !

    also note that when you specify an encoding for your .py file it just use it for that code and dont change the default system encoding.

    so do something like this :

    import codecs
    my_str='♠' # or something like my_str='\u05dd' 
    my_str.encode().decode('cp437')
    

提交回复
热议问题