What to do if Python says “character mapping must return integer, None or unicode”
So you're using the unicode class's translate method, and it says:
TypeError: character mapping must return integer,Noneorunicode
You may be wondering what causes this. After all, you're duly using the string.maketrans function. Surely this should be valid?
Well, no: You can only use string.maketrans with str.translate. For unicode.translate, you must use a different kind of translation table:
… a mapping of Unicode ordinals to Unicode ordinals [
int], Unicode strings [unicode] orNone.
In Localization Helper, I had to replace my string.maketrans call with this code:
dict(zip(map(ord, u'\a\b\t\n\v\f\r'), u'abtnvfr'))
Note that I need to call ord on each key character, because keys must be ints.