Answer the question
In order to leave comments, you need to log in
How to correctly specify the encoding in Python3?
Hello! Help me please. I'm still quite new to Python.
You need to create a transliterator from Cyrillic to Latin.
However, I am adding new characters to the alphabet, different diacritics. I am getting an encoding related error. How to fix it?
Traceback (most recent call last):
File "C:\Users\Лариса\Desktop\Фото и документы\Математика и инфа\module.py", line 56, in <module>
print(latinizator(line, legend), end='')
File "C:\Users\Лариса\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1251.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xed' in position 18: character maps to <undefined>
import os
import fileinput
def latinizator(letter, dic):
for i, j in dic.items():
letter = letter.replace(i, j)
return letter
legend = {
'Ё':'Ö',
'Ж':'Ž',
'З':'Z',
'И':'I',
'Й':'Î',
'Ч':'Č',
'Ш':'Š',
'Щ':'Ś',
'Ъ':'V',
'Ы':'Y',
'Ь':'V',
'Э':'E',
'Ю':'Ü',
'Я':'Ä',
#Дополнительные знаки
'ий':'í',
'ую':'ú',
'ой':'ó',
'ей':'é',
'ая':'á',
'ть':'ć',
}
with fileinput.FileInput('C:\\Users\\Лариса\\Desktop\\Фото и документы\\Математика и инфа\\alphabet.txt', inplace=True, backup='.bak') as f:
for line in f:
print(latinizator(line, legend), end='')
Answer the question
In order to leave comments, you need to log in
everything seems to work as intended
, both files are in utf8, but the file with texts can be made in any encoding and it is indicated when opening
import os
import fileinput
def latinizator(letter, dic):
for i, j in dic.items():
letter = letter.replace(i, j)
return letter
legend = {
'Ё':'Ö',
'Ж':'Ž',
'З':'Z',
'И':'I',
'Й':'Î',
'Ч':'Č',
'Ш':'Š',
'Щ':'Ś',
'Ъ':'V',
'Ы':'Y',
'Ь':'V',
'Э':'E',
'Ю':'Ü',
'Я':'Ä',
#Дополнительные знаки
'ий':'í',
'ую':'ú',
'ой':'ó',
'ей':'é',
'ая':'á',
'ть':'ć',
}
with open('123.txt', 'r', encoding='utf8') as f:
for line in f:
print(latinizator(line, legend), end='')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question