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


Messages In This Thread
Index Out Of Range problem - by yelyah - Dec-08-2018, 08:41 PM
RE: Index Out Of Range problem - by buran - Dec-08-2018, 08:52 PM
RE: Index Out Of Range problem - by yelyah - Dec-08-2018, 09:02 PM
RE: Index Out Of Range problem - by buran - Dec-08-2018, 09:14 PM
RE: Index Out Of Range problem - by yelyah - Dec-08-2018, 09:17 PM
RE: Index Out Of Range problem - by buran - Dec-08-2018, 09:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,460 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,582 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,973 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,399 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,480 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,287 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,905 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,366 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range Laplace12 1 2,254 Jun-22-2021, 10:47 AM
Last Post: Yoriz
  IndexError: list index out of range brunolelli 11 6,688 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