How can I display native accents to languages in console in windows?

后端 未结 3 1169
别那么骄傲
别那么骄傲 2020-12-11 23:40
print \"Español\\nPortuguês\\nItaliano\".encode(\'utf-8\')

Errors:

Traceback (most recent call last): File \"\", line 1,

相关标签:
3条回答
  • 2020-12-12 00:06

    This works for me:

    # coding=utf-8
    print "Español\nPortuguês\nItaliano"
    

    You might want to try running it using chcp 65001 && your_program.py As well, try changing the command prompt font to Lucida Console.

    0 讨论(0)
  • 2020-12-12 00:09

    First of all, in Python 2.x you can't encode a str that has non-ASCII characters. You have to write

    print u"Español\nPortuguês\nItaliano".encode('utf-8')
    

    Using UTF-8 at the Windows console is difficult.

    • You have to set the Command Prompt font to a Unicode font (of which the only one available by default is Lucida Console), or else you get IBM437 encoding anyway.
    • chcp 65001
    • Modify encodings._aliases to treat "cp65001" as an alias of UTF-8.

    And even then, it doesn't seem to work right.

    0 讨论(0)
  • 2020-12-12 00:30

    Short answer:

    # -*- coding: utf-8 -*-
    print u"Español\nPortuguês\nItaliano".encode('utf-8')
    

    The first line tells Python that your file is encoded in UTF-8 (your editor must use the same settings) and this line should always be on the beginning of your file.

    Another thing is that Python 2 knows two different basestring objects - str and unicode. The u prefix will create such a unicode object instead of the default str object, which you can then encode as UTF-8 (but printing unicode objects directly should also work).

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