Task 2
unknown
python
2 years ago
3.2 kB
6
Indexable
def repq(fstr1, fstr2):
translit_dict = {'а' : 'a',
'б' : 'b',
'в' : 'v',
'г' : 'g',
'д' : 'd',
'е' : 'ye',
'ё' : 'yo',
'ж' : 'zh',
'з' : 'z',
'и' : 'i',
'й' : 'j',
'к' : 'k',
'л' : 'l',
'м' : 'm',
'н' : 'n',
'о' : 'o',
'п' : 'p',
'р' : 'r',
'с' : 's',
'т' : 't',
'у' : 'u',
'ф' : 'f',
'х' : 'h',
'ц' : 'c',
'ч' : 'ch',
'ш' : 'sh',
'щ' : 'shch',
'ь' : '\'',
'ы' : 'y',
'ъ' : '\"',
'э' : 'e',
'ю' : 'yu',
'я' : 'ya',
'a' : 'а',
'b' : 'б',
'v' : 'в',
'g' : 'г',
'd' : 'д',
'ye' : 'е',
'yo' : 'ё',
'zh' : 'ж',
'z' : 'з',
'i' : 'и',
'j' : 'й',
'k' : 'к',
'l' : 'л',
'm' : 'м',
'n' : 'н',
'o' : 'о',
'p' : 'п',
'r' : 'р',
's' : 'с',
't' : 'т',
'u' : 'у',
'f' : 'ф',
'h' : 'х',
'c' : 'ц',
'ch' : 'ч',
'sh' : 'ш',
'shch' : 'щ',
'\'' : 'ь',
'y' : 'ы',
'\"' : 'ъ',
'e' : 'э',
'yu' : 'ю',
'ya' : 'я'
}
new_td = dict()
for td in translit_dict:
new_td[td.upper()] = translit_dict[td].upper()
translit_dict.update(new_td)
with (open(fstr1, mode='r', encoding='utf-8') as f1,
open(fstr2, mode='w', encoding='cp1251') as f2):
text = f1.read()
i = 0
new_text = ''
while i < len(text):
if text[i: i + 4] in translit_dict:
new_text += translit_dict[text[i: i + 4]]
i += 4
elif text[i: i + 2] in translit_dict:
new_text += translit_dict[text[i: i + 2]]
i += 2
elif text[i] in translit_dict:
new_text += translit_dict[text[i]]
i += 1
else:
new_text += text[i]
i += 1
f2.write(new_text)
repq('tmp1.txt', 'output1.txt')Editor is loading...
Leave a Comment