Getting python to print in UTF8 on Windows XP with the console

后端 未结 4 523
忘了有多久
忘了有多久 2021-01-02 03:18

I would like to configure my console on Windows XP to support UTF8 and to have python detect that and work with it.

So far, my attempts:

C:\\Document         


        
相关标签:
4条回答
  • 2021-01-02 03:36

    When I try the same thing on Python 2.7 I get an error on import sys:

    LookupError: unknown encoding: cp65001

    This implies to me that Python doesn't know how to work with the special Windows UTF-8 code page, and 2.5 handled the situation ungracefully.

    Apparently this was investigated and not fixed in Python 3.2: http://bugs.python.org/issue6058

    Update: In What's New In Python 3.3 it lists cp65001 support as a new feature.

    0 讨论(0)
  • 2021-01-02 03:42

    I would like to configure my console on Windows XP to support UTF8

    I don't think it's going to happen.

    The 65001 code page is buggy; some stdio calls behave incorrectly and break many tools. Whilst you can register cp65001 as an encoding manually:

    def cp65001(name):
        if name.lower()=='cp65001':
            return codecs.lookup('utf-8')
    
    codecs.register(cp65001)
    

    and this allows you to print u'some unicode string', it doesn't allow you to write non-ASCII characters in that Unicode string. You get the same odd errors (IOError 0 et al) that you do when you try to write non-ASCII UTF-8 sequences directly as byte strings.

    Unfortunately UTF-8 is a second-class citizen under Windows. NT's Unicode model was drawn up before UTF-8 existed and consequently you're expected to use two-byte-per-code-unit encodings (UTF-16, originally UCS-2) anywhere you want consistent Unicode. Using byte strings, like many portable apps and languages (such as Python) written with C's stdio, doesn't fit that model.

    And rewriting Python to use the Windows Unicode console calls (like WriteConsoleW) instead of the portable C stdio ones doesn't play well with shell tricks like piping and redirecting to a file. (Not to mention that you still have to change from the default terminal font to a TTF one before you can see the results working at all...)

    Ultimately if you need a command line with working UTF-8 support for stdio-based apps, you'd probably be better off using an alternative to the Windows Console that deliberately supports it, such as Cygwin's, or Python's IDLE or pywin32's PythonWin.

    0 讨论(0)
  • 2021-01-02 03:51

    set this in your win:

    set PYTHONIOENCODING=utf-8
    
    0 讨论(0)
  • 2021-01-02 03:53

    I had problems displaying the Euro symbol in the cmd console from a Python script using Windows Vista. Here's what worked for me:

    Fist, I need to make sure the font is set as Lucinda Console and not Raster Fonts which don't work. That can be done by setting the default properties of the console in the drop down menu of the console window and restarting the console window with cmd.exe.

    Second, when I run cmd I set the code page with chcp 1252.

    Third, I make sure my editor (Notepad++) has the right encoding settings. On the Encoding drop down menu in Notepad++ select Encode in UTF-8.

    That worked for me.

    0 讨论(0)
提交回复
热议问题