UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 4711: character maps to <undefined>

170 Views Asked by At

Soo

import disnake
from disnake.ext import commands
import time

from disnake import Intents
bot = commands.Bot(command_prefix="*", intents=disnake.Intents.all())

@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')
    channel1 = bot.get_channel(1105450189688414208)  
    channel2 = bot.get_channel(1105452174546321498)  
    last_line_file1 = ''
    last_line_file2 = ''
    while True:
        # with open("C:\\Users\\______\\____\\_____\\______\\_____\\_____\\server_log.txt", 'r', encoding='cp1251') as file1:
        #     last_line = file1.readlines()[-1].strip() символы переноса строки
        #     if last_line != last_line_file1:
        #         await channel1.send(f'```\n{last_line}\n```')
        #         last_line_file1 = last_line  
        with open('"C:\\Users\\______\\____\\_____\\______\\_____\\_____\\server_chat_log.txt"', 'r', encoding='cp1251') as file2:
            last_line = file2.readlines()[-1].strip()  
            if last_line != last_line_file2:
                await channel2.send(last_line)
                last_line_file2 = last_line 
        time.sleep(0.5)
bot.run('---')

error: line 22, in on_ready last_line = file2.readlines()[-1].strip() # Получаем последнюю строку из файла и удаляем символы переноса строки File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\encodings\cp1251.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 4711: character maps to undefined

server_chat_log.txt encoded in cp1251.

With the encoding qualifier I realized that the file is written in cp1251, but the code stops working (server_chat_log.txt there is a Cyrillic encoded)

Example of server_chat_log.txt:

[00:15:59]: [Say] (KU_LnRtDiLl) groznyjsasison: у меня норм лут теперь
[00:16:56]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): скок у тебя камня?
[00:17:05]: [Say] (KU_LnRtDiLl) groznyjsasison: камня 14
[00:17:06]: [Join Announcement] Aleksey Meatballs in DST
[00:17:09]: [Say] (KU_LnRtDiLl) groznyjsasison: кремня 24
[00:17:13]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): добудь 40 хотя бы
[00:17:17]: [Say] (KU_LnRtDiLl) groznyjsasison: знаю
[00:17:18]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): кремня итак у меня 40
[00:19:42]: [Say] (KU_LnRtDiLl) groznyjsasison: давай я угля добуду
[00:19:46]: [Say] (KU_LnRtDiLl) groznyjsasison: для казана
[00:19:49]: [Say] (KU_l2ws0ArT) РљРѕРЅРЅРѕСЂ RK800 (????): РіРѕ
[00:19:59]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): я золото добыл
[00:20:09]: [Say] (KU_LnRtDiLl) groznyjsasison: у меня 2 золота
[00:20:24]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): добавь меня в др
[00:20:31]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): нах ето говорить в общий чат
[00:20:38]: [Say] (KU_LnRtDiLl) groznyjsasison: как
[00:20:40]: [Say] (KU_LnRtDiLl) groznyjsasison: РІ РґСЂ
[00:20:41]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): в стиме
[00:20:47]: [Say] (KU_LnRtDiLl) groznyjsasison: СЏ РЅРµ РјРѕРіСѓ
[00:20:52]: [Say] (KU_LnRtDiLl) groznyjsasison: РЅРµ РґРѕРЅРёР»
[00:20:52]: [Say] (KU_l2ws0ArT) Коннор RK800 (????): я тебе заявку кину
0

There are 0 best solutions below