Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Index Out Of Range problem
#1
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]
Reply
#2
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?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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"
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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)
Reply
#6
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,356 May-14-2023, 06:43 AM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,417 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,929 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,363 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,450 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,233 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,863 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,326 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,233 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,579 Mar-25-2021, 11:36 PM
Last Post: brunolelli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020