Good afternoon I read the utf8-file and output it to the console. When I try to print the letter "I", an error occurs:
File "I:\ProgramFile\Anaconda\lib\encodings\cp1251.py", line 15, in decode return codecs.charmap_decode(input,errors,decoding_table) UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 1: character maps to < undefined > Reproduced by this example:
test_text_1 = "Задача\n" test_text_2 = "Итератор" file = open('temp.txt', 'w', encoding='utf-8') file.write(test_text_1) file.write(test_text_2) file.close() text = open('temp.txt', 'rb') for byte_code in text: print(byte_code.strip()) test_text = byte_code.decode('cp1251') print(test_text.strip()) The first word is displayed normally, and the second is an error. I just can not find a way to overcome the problem.
UPD: Apparently, I described the problem too widely, correct:
How to convert "And" from utf-8 to cp1251? For "A" everything works, but for "I" it does not.
Code:
byte1 = 'А'.encode('utf-8') byte2 = 'И'.encode('utf-8') print(byte1, byte2) test1 = byte1.decode('cp1251') print(test1) test2 = byte2.decode('cp1251') print(test2)
'текст'.encode('cp1251'). As a result, the bytes are obtained, which can already be applied where you need. - insolor