1

我正在创建一个简单的游戏,旨在提示用户输入英语单词的希腊语翻译。例如:

cow: # here, the gamer would answer with *η αγελάδα* in order to score one point.

我使用辅助函数从 txt 文件读取和解码。我在所述函数中使用以下代码:

# The variable filename refers to my helper function's sole parameter, it takes the 
# above mentioned txt file as an argument.
words_text = codecs.open(filename, 'r', 'utf-8')

然后这个辅助函数读取每一行。这些行类似于这样:

# In stack data, when I debug, it reads as u"\η αγελάδα - cow\r\n".
u"\u03b7 \u03b1\u03b3\u03b5\u03bb\u03ac\u03b4\u03b1 - cow\r\n"

然而,读取时文件的第一行有一个不需要的前缀 ueff-:

# u"\ufeffη αγελάδα - cow\r\n"
u"\ufeff\u03b7 \u03b1\u03b3\u03b5\u03bb\u03ac\u03b4\u03b1 - cow\r\n"

注意:查看 Mark 的回答后,我发现前面的 oject (ueff) 是 BOM 签名(用于区分 UTF-8 和其他编码)。

这是一个小问题,我不知道如何以最整洁的方式将其删除。无论如何,我的辅助函数然后创建并返回一个新字典,看起来像这样:

{u'\u03b7 \u03b1\u03b3\u03b5\u03bb\u03ac\u03b4\u03b1': 'cow'}

然后,在我的主要功能中,我使用以下内容来存储用户的输入:

# This is the code for the prompt I noted at the beginning.
# The variable gr_en_dict is the dictionary noted right above.
for key in gr_en_dict:
    user_reply = raw_input('%s: ' % (gr_en_dict[key])).decode(sys.stdout.encoding)

然后,我将用户输入的值与字典中的相应键进行比较:

# I imported unicodedata as ud.
if ud.normalize('NFC', user_reply) == ud.normalize('NFC', key):
        score += 1

在回答与我类似的问题时,用户 ΤZΩΤZΙΟΥ 说要导入模块 unicodedata 并调用 normalize 方法(我在上面的代码中做了),但我怀疑这可能没有必要。不幸的是,程序的这一步还没有关系,因为我在解码用户的输入时遇到了问题。为了演示,当我打印 user_reply 的规范字符串表示和字典中相应键的表示 [使用内置的 repr()] 时,我得到以下结果:

用户的输入(user_reply):

u'? \u03b1?\u03b5??\u03b4\u03b1'

如果我在没有 repr() 函数的情况下打印用户的输入,它看起来像这样:

? α?ε??δα

在我的字典中键入:

u'\u03b7 \u03b1\u03b3\u03b5\u03bb\u03ac\u03b4\u03b1'

如果我在没有 repr() 的情况下打印它,我会收到一个错误:

UnicodeEncodeError: 'charmap' codec can't encode character u'\u03b7' in position 0: character maps to <undefined>

请注意用户输入中的问号以及当我尝试正确打印希腊语单词时出现的错误。这似乎是我问题的症结所在。

那么,为了解码用户的输入并正确显示所有希腊字符,我究竟需要做什么?

使用我的本机代码页时:

C:\>chcp
Active code page: 437

C:\>\python25\python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'cp437'
>>> print '? α?ε??δα'
? α?ε??δα
>>>

使用希腊语代码页时:(奇怪的是,只有当我先将其复制到剪贴板然后将其粘贴到单词类型应用程序中时,它才会正确显示。我会在默认控制台中发布它实际打印的图像,但我缺少这样做的声誉。)

C:\>chcp 869
Active code page: 869

C:\>\python25\python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'cp869'
>>> print ' η αγελάδα'
 η αγελάδα
>>> print 'η αγελάδα'
η αγελάδα
>>>

UP:我不得不将默认控制台的字体更改为 Lucida Console。这解决了我的分歧。

4

2 回答 2

4

对于您的部分问题,请使用:

words_text = codecs.open(filename, 'r', 'utf-8-sig')

它将处理 \ufeff 的字节顺序标记。

从技术上讲,这是:

user_reply = raw_input('%s: ' % (gr_en_dict[key])).decode(sys.stdout.encoding)

应该:

user_reply = raw_input('%s: ' % (gr_en_dict[key])).decode(sys.stdin.encoding)

但实际上它们应该是相同的编码。

我认为问题在于您的默认控制台中的编码不支持所有希腊字符。当我更改为希腊代码页时,事情开始变得更好。请注意,我可以将正确的字符粘贴到下面的print语句中,但 cp437 实际上并不支持所有字符,因此打印时不支持的字符将替换为问号:

C:\>chcp
Active code page: 437

C:\>python
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'cp437'
>>> print 'η αγελάδα - cow'
? α?ε??δα - cow

如果我切换到希腊代码页(869 或 1253),它可以工作:

C:\>chcp 869
Active code page: 869

C:\>python
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'cp869'
>>> print 'η αγελάδα - cow'
η αγελάδα - cow
>>>
于 2011-05-27T00:26:05.180 回答
1

标准的 windows shell 存在扩展字符的问题。我建议使用类似 Windows PowerShell 的东西。

对于 '\ufeff' 字符,即字节顺序标记,您可以在读入文件后执行以下检查:

words_text = codecs.open(filename, 'r', 'utf-8')
words_text_lines = words_text.readlines()

if words_text_lines and words_text_lines[0][0]==unicode(codecs.BOM_UTF8, 'utf8'):
    words_text_lines[0] = words_text_lines[0][1:]

这样,如果它在那里,您将丢弃它。

于 2011-05-27T04:09:39.157 回答