【发布时间】:2013-10-20 22:44:14
【问题描述】:
我想用 Python 3 编写一个应该打印字母的应用程序:æ、å、ø。使用 Python IDLE 时,一切运行良好,但我需要此应用程序才能在终端(Windows 7)中工作。 Python 显示以下字母:ł、ę、ą、ś、ć、ź、ż 和 ó,因为它在 IDLE 和控制台中都应如此。但我的应用程序需要处理所有这些字母。 我收到此错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\xe5' in position 0: cha
racter maps to <undefined>
\xe5 应该是 å。 我应该怎么做才能让它发挥作用?
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()
“tavla”脚本没有问题:
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))
问题出在这一行:
print ("Trykk på ein knapp for å fortsetta...")
因为它使用字母“å”。按照意图,tavla 应显示所有字母: æ, å, ø, é ł, ę, ą, ś, ć, ź, ż, ó
# -*- coding: utf-8 -*-
根本没有改变任何东西。还是一样。
【问题讨论】:
-
如果将
# -*- coding: utf-8 -*-放在文件顶部会发生什么? -
我们需要查看您的
tavla模块,因为您的这部分代码没有问题。 -
如果我将
# -*- coding: utf-8 -*-行放在脚本的顶部,print()行适用于 Windows XP。其余代码我不知道,因为我不懂提示,也不知道如何响应。
标签: python windows encoding terminal