Python 3 与 Python 2 中的 Unicode 字符

Unicode characters in Python 3 vs Python 2

我在python2 中写了一个简单的脚本来训练我对平假名字母的认识。请看下面的代码:

import random

ROMAJI = ['A','I','U','E','O','KA','KI','KU','KE','KO','SA','SHI','SU','SE','SO','TA','CHI','TSU','TE','TO','NA','NI','NU','NE','NO']

romaji_letter = random.choice(ROMAJI)
romaji_index = ROMAJI.index(romaji_letter)

A = u'\u3042'
I = u'\u3044'
U = u'\u3046'
E = u'\u3048'
O = u'\u304A'
KA = u'\u304B'
KI = u'\u304D'
KU = u'\u304F'
KE = u'\u3051'
KO = u'\u3053'
SA = u'\u3055'
SHI = u'\u3057'
SU = u'\u3059'
SE = u'\u305B'
SO = u'\u305D'
TA = u'\u305F'
CHI = u'\u3061'
TSU = u'\u3064'
TE = u'\u3066'
TO = u'\u3068'
NA = u'\u306A'
NI = u'\u306B'
NU = u'\u306C'
NE = u'\u306D'
NO = u'\u306E'

HIRAGANA = [A,I,U,E,O,KA,KI,KU,KE,KO,SA,SHI,SU,SE,SO,TA,CHI,TSU,TE,TO,NA,NI,NU,NE,NO]

print("\nWhich Hiragana character is the Romaji letter " + romaji_letter + " ?\n")

for letter in HIRAGANA:
        print(letter.encode("UTF-8")+"     "+str(HIRAGANA.index(letter))+"\n")


print("Choose the corresponding number and press [ENTER] : ")

userInput = raw_input()

try:
        letter_index = int(userInput)
        print("You have chosen letter "+HIRAGANA[letter_index].encode("UTF-8"))

        if ( letter_index == romaji_index):
                print("The answer is correct!")
        else:
                print("The answer is incorrect!")

except:
        print("Enter only the number of the corresponding letters!")

当我在 python 2 中执行此操作时,它确实可以正常工作。但是,当我尝试使用 python 3 时,出现以下错误:

"Can't concat bytes to str" 

谁能帮忙解释一下为什么会这样?

哦,不确定这是否重要,但我在 Fedora 上使用 python 2,在 Windows 7 上使用 python 3(使用 Visual Studio 2012 以及python 工具)

您不应将字符编码为 UTF-8。将编码留给 print() 调用:

for letter in HIRAGANA:
    print(letter + u"     " + str(HIRAGANA.index(letter)) + u"\n")

和:

print(u"You have chosen letter " + HIRAGANA[letter_index])

因为Python3中的字符串都是Unicode值,不是字节值,但是编码总是产生字节。

更好的是,在此处使用字符串格式和 enumerate()

for index, letter in enumerate(HIRAGANA):
    print(u"{}     {}\n".format(letter, index))

print(u"You have chosen letter {}".format(HIRAGANA[letter_index]))

接下来,raw_input() 在 Python 3 中不可用,您必须在那里使用 input()。您可以使用异常来检测使用哪一个:

try:
    raw_input
except NameError:
    raw_input = input  # Python 3