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

Errors:

Traceback (most recent call last): File "", line 1, in print "Español\nPortuguês\nItaliano".encode('utf-8') UnicodeDecodeError: 'ascii' codec can't decode byte 0xf1 in position 4: ordinal not in range(128)

I'm trying to make a multilingual console program in Windows. Is this possible? I've saved the file in utf-8 encoding as well, I get the same error.

*EDIT I"m just outputting text in this program. I change to lucida fonts, I keep getting this: alt text http://img826.imageshack.us/img826/7312/foreignlangwindowsconso.png

I'm just looking for a portable way to correctly display foreign languages in the console in windows. If it can do it cross platform, even better. I thought utf-8 was the answer, but all of you are telling me fonts, etc.. also plays a part. So anyone have a definitive answer?

chazzycheese
  • 93
  • 1
  • 1
  • 4
  • Ok I opened, cmd.exe then type chcp 65001 This "changed the codepage" Now this code will properly display it: print u"Español\nPortuguês\nItaliano" with .py file encoded in utf-8 in the editor Now I must find a way through python to set this codepage automatically..... – chazzycheese Aug 13 '10 at 01:43
  • here's [how to do it on Python 3](http://stackoverflow.com/a/30551552/4279) – jfs Jul 23 '15 at 23:47

3 Answers3

3

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).

tux21b
  • 90,183
  • 16
  • 117
  • 101
  • i'm on windows 7, on properties ->fonts I only have: consolas, lucida, and raster fonts None of them display it correctly, i get weird symbols instead. – chazzycheese Aug 13 '10 at 01:23
  • Maybe this link might help you: http://blog.i18n.ro/using-unicode-console-output-with-python/ – tux21b Aug 13 '10 at 01:39
2

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.

dan04
  • 87,747
  • 23
  • 163
  • 198
1

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.

Dumb Guy
  • 3,336
  • 21
  • 23
  • And this will only "work" if your console is set to utf-8. Using unicode everywhere and encoding on output is a far more robust solution. – habnabit Aug 13 '10 at 01:40