Skip to content
Advertisement

Python IndexError after 2800 iterations

    import config
    
    def parse():
        chat_log = open('chat.log', "r", encoding="utf-8")
        chat = chat_log.read().split("nnn")
        chat_parsed = open("chat.txt", "w", encoding="utf-8")
        lines = []
        try:
            for i in chat:
                if len(i) > 0:
                    x = i.split(" ")
                    if config.nickname != i.split(" ")[2] and config.nickname != i.split(" ")[4] 
                            and f"{config.nickname} = #{config.channel}" not in i.split("—")[1].split(":")[2]:
                        if "PING" not in i:
                            if len(i) > 0:
                                user = i.split("—")[1].split(":")[1].split("!")[0]
                                msg = i.split("—")[1].split(":")[2]
                                line = [user, msg]
                                lines.append(line)
    
            for i in lines:
                chat_parsed.write(str(i))
                chat_parsed.write('n')
            chat_log.close()
            chat_parsed.close()
        except IndexError:
            chat_parsed.write('"[ERROR], [ERROR]"n')
            chat_log.close()
            chat_parsed.close()

For some reason that I don’t get, after calling this for the 2800th time (aprox), it gets into the except part. I’m using this to parse a chat log for easier processing later on a Twitch chat bot, so I have to call the parser a lot of times for it to be updated. Can anyone help? Thanks in advance!

Also, the error seemingly comes from the if config.nickname != i.split(" ")[2] and config.nickname != i.split(" ")[4] and f"{config.nickname} = #{config.channel}" not in i.split("—")[1].split(":")[2] part.

Advertisement

Answer

I finally got it working, the problem here was the PING from the server, it had a different format than the other lines.

That was fixed by switching around the if instructions and first checking if PING was a part of the string. I had a new problem, though, the bot disconnected after a few minutes, just about the same time that took the last problem to be noticed. I was just not responding to the ping. Thanks a lot to the last answer, it helped a lot!

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement