我需要我的代码来识别单词结尾处希伯来语中的特定字母,然后将其替换为最终字母形式。
例如:כ=ך,מ=ם,נ=ן,פ=ף,צ=ץ。
我尝试使用以下代码进行此操作:

# -*- coding: utf-8 -*-
from string import maketrans

text = "לנמנמ זה כיפ"
textSplit = text.split()
translator = maketrans("כמנפצ","ךםןףץ")
correctSpelling = ""

for i in textSplit:
    if i[-1]=="כ" or i[-1]=="מ":
        correctSpelling += i.translate(translator) + " "
    else:
        correctSpelling += i + " "

print correctSpelling


输出:לנמנמזהכיפ。
预期输出:לנמנםזהכיף。
上面的代码没有错误。
在阅读了Unicode HOWTOOvercoming frustration: Correctly using unicode in python2和每个软件开发人员的绝对最低限度后,绝对应该肯定地了解Unicode和字符集(无借口!),该问一下stackoverflow了。

最佳答案

您使用了错误的数据类型。在Python 2中,字符串(如'abc')用于字节,而unicode(如u'abc')用于文本。

所以:

# wrong (putting unicode characters in byte literals is ill-defined):
ord("צ");
# correct:
ord(u"צ");


(Python 3与此相反,'abc'是Unicode,并且字节具有显式的b前缀)

关于python - 如何更改其他语言单词的最后一个字母?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33678959/

10-15 17:42