Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python safe transliterate print (kill UnicodeEncodeError)
#!/usr/bin/env python3
# encoding=utf-8
from __future__ import print_function
from __future__ import unicode_literals
import string
ru_lang_translit_map = {
"Я": "Ja", "Ю": "Ju", 'Э': 'E', 'Ь': '\'', 'Ы': 'Y', 'Ъ': '\'', "Щ": "Sch",
"Ш": "Sh", "Ч": "Ch", "Ц": "Ts", 'Х': 'H', 'Ф': 'F', 'У': 'U', 'Т': 'T',
'С': 'S', 'Р': 'R', 'П': 'P', 'О': 'O', 'Н': 'N', 'М': 'M', 'Л': 'L',
'К': 'K', 'Й': 'J', 'И': 'I', 'З': 'Z', "Ж": "Zh", 'Ё': 'E', 'Е': 'E',
'Д': 'D', 'Г': 'G', 'В': 'V', 'Б': 'B', 'А': 'A', "я": "ja", "ю": "ju",
'э': 'e', 'ъ': '\'', 'ы': 'y', 'ь': '\'', "щ": "sch", "ш": "sh", "ч": "ch",
"ц": "ts", 'х': 'h', 'ф': 'f', 'у': 'u', 'т': 't', 'с': 's', 'р': 'r',
'п': 'p', 'о': 'o', 'н': 'n', 'м': 'm', 'л': 'l', 'к': 'k', 'й': 'j',
'и': 'i', 'з': 'z', "ж": "zh", 'ё': 'e', 'е': 'e', 'д': 'd', 'г': 'g',
'в': 'v', 'б': 'b', 'а': 'a',
}
en_lang_translit_map = {}
for x in string.ascii_letters:
en_lang_translit_map[x] = x
digit_translit_map = {}
for x in string.digits:
digit_translit_map[x] = x
punctuation_and_whitespaces_translit_map = {}
for x in string.punctuation:
punctuation_and_whitespaces_translit_map[x] = x
for x in string.whitespace:
punctuation_and_whitespaces_translit_map[x] = ' '
allowed_filename_punctuation_and_whitespaces_translit_map = {}
for x in "!#$%&'()+,-.;=@[]^_`{}~":
allowed_filename_punctuation_and_whitespaces_translit_map[x] = x
for x in string.whitespace:
allowed_filename_punctuation_and_whitespaces_translit_map[x] = ' '
allowed_filename_translit_map = \
allowed_filename_punctuation_and_whitespaces_translit_map + \
ru_lang_translit_map + en_lang_translit_map + \
digit_translit_map
ascii_translit_map = punctuation_and_whitespaces_translit_map + \
en_lang_translit_map + \
digit_translit_map
ru_ascii_translit_map = ascii_translit_map + ru_lang_translit_map
do_skip_char = lambda c: ''
def transliterate(source_str, translit_map=ru_ascii_translit_map,
do_if_unknown_char=do_skip_char):
return ''.join(map(
lambda x: translit_map.get(x, do_if_unknown_char(x)),
source_str))
def safe_print(*args, **kwargs):
try:
print(*args, **kwargs)
except UnicodeEncodeError:
translited_args = tuple(_safe_transliterate(arg) for arg in args)
print(*translited_args, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.