How to display utf-8 in windows console

前端 未结 4 604
予麋鹿
予麋鹿 2020-11-27 06:39

I\'m using Python 2.6 on Windows 7

I borrowed some code from here: Python, Unicode, and the Windows console

My goal is t

4条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 07:02

    Never ever ever use setdefaultencoding. If you want to write unicode strings to stdio, encode them explicitly. Monkeying around with setdefaultencoding will cause stdlib modules and third-party modules alike to break in horrible subtle ways by allowing implicit conversion between str and unicode when it shouldn't happen.

    Yes, the problem is most likely that your code page isn't set properly. However, using os.popen won't change the code page; it'll spawn a new shell, change its code page, and then immediately exit without affecting your console at all. I'm not personally very familiar with windows, so I couldn't tell you how to change your console's code page from within your python program.

    The way to properly display unicode data via utf-8 from python, as mentioned before, is to explicitly encode your strings before printing them: print s.encode('utf-8')

提交回复
热议问题