Windows Console Encoding

What is standard console encoding in Windows? It seems that sometimes it is ANSI encoding ( CP-1252 ), sometimes it is OEM encoding ( CP-850 for Western Europe by default) specified by the command chcp.

  • Command line arguments and environment variables trigger ANSI encoding ( é= 0xe9):

    > chcp 850
    Active code page: 850
    > python -c "print 'é'"
    Ú
    > python -c "print '\x82'"
    é
    > python -c "print '\xe9'"
    Ú
    > $env:foobar="é"; python -c "import os; print os.getenv('foobar')"
    Ú
    
    > chcp 1252
    Active code page: 1252
    > python -c "print 'é'"
    é
    > python -c "print '\x82'"
    ,
    > python -c "print '\xe9'"
    é
    > $env:foobar="é"; python -c "import os; print os.getenv('foobar')"
    é
    
  • The Python console and standard input trigger OEM encoding ( é= 0x82 if OEM encoding is CP-850, é= 0xe9 if OEM encoding is CP-1252):

    > chcp 850
    Active code page: 850
    > python
    >>> print 'é'
    é
    >>> print '\x82'
    é
    >>> print '\xe9'
    Ú
    > python -c "print raw_input()"
    é
    é
    
    > chcp 1252
    Active code page: 1252
    > python
    >>> print 'é'
    é
    >>> print '\x82'
    ,
    >>> print '\xe9'
    é
    > python -c "print raw_input()"
    é
    é
    

Note. - In these examples, I used Powershell 5.1 and CPython 2.7.14 for Windows 10.

0
source share
1

, , ASCII, Windows, , Python . , Python. Python ( 0-255), print , Python.

Powershell, , Python , chcp, Language -Unicode, ( , "" ). é 0xE9 Python . Windows, 0xE9 é ( no , ANSI).

. Python Windows, MBCS; Unicode 'mbcs', MultiByteToWideChar() WideCharToMultiByte() API Windows CP_ACP.

Python Powershell, chcp. 850 0x82 é. print 0x82 , 0x82 é .

Unicode ( unicode, u'é'), Python . print sys.stdout, Unicode ( PYTHONIOENCODING, ), print u'é' Unicode sys.stdout, , .

unicode u'é' ( ), Python . -c , Latin-1. . , python -c "print u'é'" print u'é' .

, Python 3 Unicode , Python "" API- Windows UTF-16, Unicode. - , Python 3.6 stdin/stdout/stderr, UTF- 8 ( "" API).

+1

Source: https://habr.com/ru/post/1617591/


All Articles