Python 3.x: Using string.maketrans() in order to create a unicode-character transformation

python, python-3.x, string, unicode

Solution

You need to use str.maketrans(), which takes two str as arguments.

>>> frm = 'acdefhnoprstuw'
>>> to = 'אקדיפהנופרסתאו'
>>> trans_table = str.maketrans(frm, to)
>>> hebrew_phrase = 'fear cuts deeper than swords'.translate(trans_table)
>>> hebrew_phrase
'פיאר קאתס דייפיר תהאנ סוורדס'

String.maketrans still existed in Python 3.1, but that's just because they missed moving it to bytes.maketrans() in 3.0. It was deprecated in 3.1 already and in 3.2 it is gone.

Problem

I would like to write the following code: ``` import string frm = b'acdefhnoprstuw' to = 'אקדיפהנופרסתאו' trans_table = string.maketrans(frm, to) hebrew_phrase = 'fear cuts deeper than swords'.translate(trans_table) ``` The above code doesn't work because the `to` parameter to `string.maketrans(frm, to)` has to be a byte sequence, not a string. The problem is that byte sequences can only contain ASCII literal characters. Therefore I cannot make a transformation which translates English strings to Hebrew strings. The reason is that `string.maketrans()` retruns a bytes object. Is there an elegant way to use the `string.maketrans()` and `translate()` functions (or equivalent functions that work with unicode) for my task?

Original source