1

I want to write an application in Python 3 that should print letters: æ, å, ø. When using the Python IDLE, everything runs great, but I need this application to work in the terminal (Windows 7). Python shows following letters: ł, ę, ą, ś, ć, ź, ż, and ó as it should both in IDLE and the console. But my app needs to work with all these letters. I get this error:

UnicodeEncodeError: 'charmap' codec can't encode character '\xe5' in position 0: cha racter maps to <undefined>

\xe5 should be å. What should I do to make it work?

import sys, os, msvcrt
import tavla

def make():
    os.system("cls")
    get_verb = input("Angje verbet (separer med komma): ")
    polish = input("Angje det polske ordet: ")
    verb = get_verb.split(",")
    try:
        tavla.tavla(verb[0],verb[1],verb[2],verb[3], polish)
        print ("Trykk på ein knapp for å fortsetta...")
        msvcrt.getch()
    except IndexError:
        sys.exit(2)

if __name__ == '__main__':
    make()

The "tavla" script causes no problems:

def tavla(ubund_sing="et hus", bund_sing="huset", ubund_pl="hus", bund_pl="husene", polsk="dom"):
        a = "\t|{0}|\t|{1}|\t\t|{2}|\t\t|{3}|".format(ubund_sing, bund_sing, ubund_pl, bund_pl)
        print(a)
        print("\n\t\t\t\t{0}".format(polsk))

The problem is this line:

        print ("Trykk på ein knapp for å fortsetta...")

because it uses the letter "å". Intended, tavla should show all the letters: æ, å, ø, é ł, ę, ą, ś, ć, ź, ż, ó

# -*- coding: utf-8 -*-

doesn't change anything at all. Still the same.

user2901218
  • 35
  • 1
  • 4
  • 1
    What happens if you put `# -*- coding: utf-8 -*-` at the top of your file? – Claudiu Oct 20 '13 at 23:38
  • We would need to see your `tavla` module, since there are no problems in this part of your code. – This company is turning evil. Oct 20 '13 at 23:39
  • The `print()` line works for me on Windows XP if I put the `# -*- coding: utf-8 -*-` line in at the top of the script. I don't know about the the rest of the code because I don't understand the prompts and know how to respond. – martineau Oct 21 '13 at 00:45
  • See answers to the question [_Unicode characters in Windows command line - how?_](http://stackoverflow.com/questions/388490/unicode-characters-in-windows-command-line-how). – martineau Oct 29 '13 at 22:58

1 Answers1

2

Try this

import sys
reload(sys)
sys.setdefaultencoding('UTF-8')

Or you can refer to http://code.activestate.com/recipes/466341-guaranteed-conversion-to-unicode-or-byte-string/

Herrington Darkholme
  • 5,979
  • 1
  • 27
  • 43