5

The title explains it well. I have set up Notepad++ to open the Python script in the command prompt when I press F8 but all Swedish characters looks messed up when opening in CMD but perfectly fine in e.g IDLE.

This simple example code:

#!/usr/bin/env python
#-*- coding: UTF-8 -*-
print "åäö"

Looks like this.

As you can see the output of the batch file I use to open Python in cmd below shows the characters correctly but not the Python script above it. How do I fix this? I just want to show the characthers correctly I dont necessarily have too use UTF-8.

I open the file in cmd using this method.

Update: Solved. Added a "chcp 1252" line at the top of the batch file and then a cls line below it to remove the message about what character encoding it uses. Then I used "# -- coding: cp1252 --" in the python script and changed the font in cmd to Lucida Console. This is done by clicking the cmd icon at the top right of the cmd window and go into properties.

Ogglas
  • 62,132
  • 37
  • 328
  • 418
Alex
  • 731
  • 1
  • 11
  • 24
  • Use a hex editor to check what the actual encoding of the non-ASCII characters is on disk. If you're declaring it to be UTF-8 when it isn't, things just won't work. – Donal Fellows Apr 17 '10 at 22:51
  • Alex: Note that what you did now is to skip Unicode use entirely. If your script needs more than the small number of characters available in such legacy encodings, then you shouldn't do this. Th proper way is to simply output Unicode in the way the console expects it—namely UTF-16. Python has a Unicode string type for a purpose. Abusing implicitly-defined codepages/encodings for a byte string type leads to all kinds of weird things, as you see here. – Joey Apr 18 '10 at 13:00
  • Ok, but how exactly is this done? Just adding a line, adding the u before the strings or both? Also if I'm correct the windows cmd console is buged compared to linux, is this right? I can't seem to get it right with any of the above methods what is the right one? – Alex Apr 18 '10 at 15:23
  • Related. • [What encoding to get Å Ä Ö to work](https://superuser.com/q/675369) • [Using UTF-8 Encoding (CHCP 65001) in Command Prompt](https://stackoverflow.com/q/57131654) • [How to use unicode characters in Windows command line](https://stackoverflow.com/q/388490) • [chcp 65001 and a .bat file](https://stackoverflow.com/q/32182619) • [Making Swedish characters show properly in Windows Command Prompt](https://stackoverflow.com/q/2660264) – Henke Jan 31 '23 at 15:53
  • Good that you don't use CodePage 65001 (UTF-8). Micrsoft hasn't done their homework: [see this comment](https://superuser.com/q/269818#comment1627713_269818). – Henke Mar 06 '23 at 17:25

4 Answers4

4

You're printing out UTF-8 bytes, but your console is not set to UTF-8. Either write Unicode as UTF-16, or set your console codepage to UTF-8.

print u"åäö"
Joey
  • 344,408
  • 85
  • 689
  • 683
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Simply writing print "åäö" in a cmd window after starting in it python works and also print u"åöä" but not saving a file. The file only contains print u"åäö" and produces an error. – Alex Apr 17 '10 at 22:28
  • That's because the Python REPL decodes it using the console's encoding when you enter it into the console. – Ignacio Vazquez-Abrams Apr 17 '10 at 23:01
1

I had the same issue and I used cp1252

C:>chcp 1252

This made the console to use 1252 encoding and then I ran my program which displayed the swedish characters with mercy.

Marko
  • 20,385
  • 13
  • 48
  • 64
AMKhan
  • 131
  • 1
  • 4
  • I tried putting `chcp 1252` inside my batch file (just above the line containing Swedish letters) and it works just fine. The printout when running the batch file **does** include a line `Active code page: 1252`. But for my purposes that is not a problem. – Henke Jan 25 '22 at 11:13
0

Set coding to: # -*- coding: ISO-8859-1 -*-

This worked for me and I tried a lot of different solutions to get it to work with Visual Studio IDE for Python.

# -*- coding: ISO-8859-1 -*-
print ("åäö")
Ogglas
  • 62,132
  • 37
  • 328
  • 418
0

Python will normally convert Unicode strings to the Windows console's encoding. Note that to use Unicode properly, you need Unicode strings (e.g., u'string') and need to declare the encoding the file is saved in with a coding: line.

For example, this (saved in UTF-8 as x.py on my system):

# coding: utf8
print u"åäö"

Produces this:

C:\>chcp
Active code page: 437

C:\>x
åäö

You'll only be able to successfully print characters that are supported by the active code page.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251