如何将希腊语字符转换为英语python

Geo*_*ge 4 python

我想要一些 Python 代码,它接受希腊语句子并将其转换为英语字符。任何人都可以帮忙吗?

nor*_*ok2 7

I am not sure at what level you want the conversion to happen, but if you want to use a character translation for which you have control over, you could use str.maketrans() and str.translate() methods.

\n\n

The first method generate a translation table according to the specifications of its argument, where the characters of the first argument are replaced with characters from the second argument (and the third argument specifies which characters to delete).\nSuch a translation table is used with str.translate(), e.g.:

\n\n
greek_alphabet = \'\xce\x91\xce\xb1\xce\x92\xce\xb2\xce\x93\xce\xb3\xce\x94\xce\xb4\xce\x95\xce\xb5\xce\x96\xce\xb6\xce\x97\xce\xb7\xce\x98\xce\xb8\xce\x99\xce\xb9\xce\x9a\xce\xba\xce\x9b\xce\xbb\xce\x9c\xce\xbc\xce\x9d\xce\xbd\xce\x9e\xce\xbe\xce\x9f\xce\xbf\xce\xa0\xcf\x80\xce\xa1\xcf\x81\xce\xa3\xcf\x83\xcf\x82\xce\xa4\xcf\x84\xce\xa5\xcf\x85\xce\xa6\xcf\x86\xce\xa7\xcf\x87\xce\xa8\xcf\x88\xce\xa9\xcf\x89\'\nlatin_alphabet = \'AaBbGgDdEeZzHhJjIiKkLlMmNnXxOoPpRrSssTtUuFfQqYyWw\'\ngreek2latin = str.maketrans(greek_alphabet, latin_alphabet)\n\nprint(\'\xce\xb1\xcf\x85\xcf\x84\xce\xbf \xce\xb5\xce\xb9\xce\xbd\xce\xb1\xce\xb9 \xce\xb5\xce\xbd\xce\xb1 \xcf\x80\xce\xb1\xcf\x81\xce\xb1\xce\xb4\xce\xb5\xce\xb9\xce\xb3\xce\xbc\xce\xb1\'.translate(greek2latin))\n# auto einai ena paradeigma\n
Run Code Online (Sandbox Code Playgroud)\n\n

请注意,虽然这使您可以更好地控制替换,但它不会为您提供相似字符的隐式翻译。例如\xce\xac, 和\xce\xb1是两个单独的字符,并且需要在转换表中单独的条目。

\n


小智 6

unidecode 模块就能解决这个问题。尝试这个:

\n\n
from unidecode import unidecode\n\ns = "\xce\xb1\xcf\x85\xcf\x84\xcf\x8c \xce\xb5\xce\xaf\xce\xbd\xce\xb1\xce\xb9 \xce\xad\xce\xbd\xce\xb1 \xcf\x80\xce\xb1\xcf\x81\xce\xac\xce\xb4\xce\xb5\xce\xb9\xce\xb3\xce\xbc\xce\xb1"\ns = unidecode(s)\nprint(s)\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
auto einai ena paradeigma\n
Run Code Online (Sandbox Code Playgroud)\n