0

I am following this example How can I display native accents to languages in console in windows? but i get an error message every time i run my file.py with the command prompt of windows.

I wish to print

print u"Università".encode('utf-8')

and the error is:

  File "C:\Users\samsung>C:\PythonScript\Script_fun\uni\uni.py", line 270
SyntaxError: Non-ASCII character '\xc3' in file C:\Users\samsung>C:\PythonScript\Script_fun\uni\uni.py on line 270, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Moreover, adding # coding=utf-8 as the first line i have the following problem

Università

instead of

Università
Community
  • 1
  • 1
Gianni Spear
  • 7,033
  • 22
  • 82
  • 131
  • 1
    Did you read PEP 263 like your error message told you to? – Wooble Sep 03 '13 at 13:06
  • i am honest yes but i did not understand. Sorry. – Gianni Spear Sep 03 '13 at 13:09
  • The second line of your Python file should be `#coding=utf-8` (assuming you've saved your file in UTF-8; your encoding may vary.) – Wooble Sep 03 '13 at 13:11
  • Not sure if this can help but have you added this at the top of your file : # -*- coding: utf-8 -*- – Freelancer Sep 03 '13 at 13:11
  • @Wooble using your solution I have the posted above (Universit├á instead of Università) – Gianni Spear Sep 03 '13 at 13:26
  • How did you create this file? – Wooble Sep 03 '13 at 13:27
  • @freelancer using your solution I have the posted above (Universit├á instead of Università) – Gianni Spear Sep 03 '13 at 13:27
  • @Gianni have you followed all the instructions of this answer : http://stackoverflow.com/a/3473206/1612326 have you setted your console as in the answer ? – Freelancer Sep 03 '13 at 13:35
  • Give up. Even if you did it right, you wouldn't reliably be able to `print` characters not in your code page to the Windows console. There are serious bugs in code page 65001. It is not worth the effort. Think of something else to do. – bobince Sep 03 '13 at 18:21
  • On Python 3, you could [use this solution](http://stackoverflow.com/a/30551552/4279) – jfs Jul 23 '15 at 23:54

1 Answers1

1

Try this:

print u"Università".encode('utf-8')
print u"Università".encode('437')
print u"Università".encode('850')
print u"Università".encode('1252')

If your system locale is en-US, then the second line (437) will render correctly.

If you now run:

chcp 1252

then the last line will look good.

That's because the Windows console does not use UTF-8 (and, in general, s*cks at international support)

You can to run this:

chcp 65001

to set the console to UTF-8 (that's what 65001 means in Windows numeric code pages), but now Python chokes (2.7, I did not try 3). And that's because Python 2.7 s*cks at international support :-)

Mihai Nita
  • 5,547
  • 27
  • 27