Python Forum

Full Version: Index Out Of Range problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys! I am working on a code that takes lines from ASC log file that logs CAN messages from vehicle. I get this error even tho my programs runs good. I would appreciate any help given here to resolve this issue. Thank you!

Quote:Traceback (most recent call last):
File "C:/Users/Luka/Desktop/Diplomski/CAN_parser.py", line 290, in <module>
main()
File "C:/Users/Luka/Desktop/Diplomski/CAN_parser.py", line 285, in main
message_type_recognizer(message_split)
File "C:/Users/Luka/Desktop/Diplomski/CAN_parser.py", line 219, in message_type_recognizer
elif can_message[4] == "d":
IndexError: list index out of range

This is example of message where "d" is on position 4.
Quote:['20.688593', '1', '9', 'Rx', 'd', '8', '04', '00', '00', '00', '00', '00', '00', '00', 'Length', '=', '242000', 'BitCount', '=', '125', 'ID', '=', '9']

And in the next quotes is way to get this error.

Firstly program is reading file line by line and splitting it in a list like is quoted up there.

def main():
    line_counter = 1
    # ASCOpen = open(ASCFilePath, 'r')
    with open(ASCFilePath) as ASCOpen:
        for line in ASCOpen:
            line = line.strip()
            message_split = line.split(' ')
            message_split = list(filter(lambda a: a != '', message_split))
            print(line_counter)
            print(message_split)
            message_type_recognizer(message_split)
            line_counter = line_counter + 1
This is part of code where it says index out of range.
def message_type_recognizer(can_message):

    if can_message[0] == "date" or can_message[0] == "base" or can_message[0] == "internal" or can_message[0] == "//" or can_message[0] == "Begin" or can_message[1] == "Start":
        header_parser(can_message)

    elif can_message[4] == "d":
        print(len(can_message))
        can_message_parser(can_message)
And now the fun part where I think is mistake but I am not sure. the next part of the code takes message and if (i.e can_message[5] = 8 then connect next 8 values in one string like '04', '00', '00', '00', '00', '00', '00' connect them in 04000.. )

def can_message_parser(can_message):

    can_payload_number = int(can_message[5])
    starting_range = 6
    ending_range = starting_range + can_payload_number - 1
    can_payload_value = ""

    for x in range(starting_range, ending_range + 1):
        can_payload_value = can_payload_value + can_message[starting_range]
        starting_range = starting_range + 1

    length_position = 6 + int(can_message[5]) + 2
    bitcount_position = length_position + 3
    CANMessageObject = CANDataMessage(can_message, can_payload_value, length_position, bitcount_position)
And down below is class for one CANDataMessage
class CANDataMessage:

    def __init__(self, can_msg, can_payload_number, length_position, bit_count_position):
        self.Time = can_msg[0]
        self.Channel = can_msg[1]
        self.ID = can_msg[2]
        self.Dir = can_msg[3]
        self.FrameType = can_msg[4]
        self.Dlc = can_msg[5]
        self.Payload = can_payload_number
        self.Length = can_msg[length_position]
        self.BitCount = can_msg[bit_count_position]
The problem is at some point your can_message has len < 5 thus the index error
what is the output from print(message_split) just before the error?
This is output of print(message_split) in main program before error:
Quote:['20.678080', '1', '9', 'Rx', 'd', '8', '04', '00', '00', '00', '00', '00', '00', '00', 'Length', '=', '242000', 'BitCount', '=', '125', 'ID', '=', '9']

This is output of print(can_message) in message_type_recognizer
Quote:['20.688593', '1', '9', 'Rx', 'd', '8', '04', '00', '00', '00', '00', '00', '00', '00', 'Length', '=', '242000', 'BitCount', '=', '125', 'ID', '=', '9']

It even prints after elif condition can_message[4] as value "d"
(Dec-08-2018, 09:02 PM)yelyah Wrote: [ -> ]It even prints after elif condition can_message[4] as value "d"
well, if it raise error on that line, it cannot print after that :-)

EDIT: can you upload sample file that you parse and that cause the error
(Dec-08-2018, 09:14 PM)buran Wrote: [ -> ]
(Dec-08-2018, 09:02 PM)yelyah Wrote: [ -> ]It even prints after elif condition can_message[4] as value "d"
well, if it raise error on that line, it cannot print after that :-)

EDIT: can you upload sample file that you parse and that cause the error

Yeeep I just got idea what could be the problem and it seems that if message is shorter then 5 elements it will make this error.

Quote:0.001051 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.001301 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.001549 1 6 Rx d 8 03 00 00 00 00 00 00 00 Length = 240000 BitCount = 124 ID = 6
0.001800 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.002050 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
2.5158 1 OverloadFrame

because my recognizer function is

def message_type_recognizer(can_message):

    if can_message[0] == "date" or can_message[0] == "base" or can_message[0] == "internal" or can_message[0] == "//" or can_message[0] == "Begin" or can_message[1] == "Start":
        header_parser(can_message)

    elif can_message[4] == "d":
        can_message_parser(can_message)

    elif can_message[4] == "r":
        can_remote_frame_parser(can_message)

    elif can_message[2] == "ErrorFrame":
        can_error_frame_parser(can_message)

    elif can_message[2] == "Statistic:":
        can_statistic_event_parser(can_message)

    elif can_message[3] == "Status:chip":
        can_error_event_parser(can_message)

    elif can_message[8] == "TxErr:":
        can_error_event_parser(can_message)

    elif can_message[2] == "OverloadFrame":
        can_overload_frame_parser(can_message)

    else:
        can_end_triggerblock_parser(can_message)
(Dec-08-2018, 09:17 PM)yelyah Wrote: [ -> ]2.5158 1 OverloadFrame
I think now you see it for yourself

(Dec-08-2018, 09:17 PM)yelyah Wrote: [ -> ]Yeeep I just got idea what could be the problem and it seems that if message is shorter then 5 elements it will make this error.
That's exactly what I said in my first post
(Dec-08-2018, 08:52 PM)buran Wrote: [ -> ]The problem is at some point your can_message has len < 5 thus the index error