IndexError: list index out of range - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: IndexError: list index out of range (/thread-32155.html) |
IndexError: list index out of range - ramu4651 - Jan-24-2021 Hi Team, I am facing below error while executing the code . Dont know where exactly the error exists . Could you please help me on this #Reading Data from Files into Collections orders_file = open('/content/sample_data/orders.txt') orders_raw = orders_file.read() # here orders_raw is read as String file orders = orders_raw.splitlines() # It will convert each row into string (orders) and put it in list #len(orders) #orders[0] orders[0:10] #for i in orders[0:10]: print(i) def get_customer_orders(orders,customer_id): orders_filtered = [] for i in orders: if int(i.split(',')[2])==customer_id: orders_filtered.append(i) return orders_filteredError Details --------------------------------------------------------------------------- Data ----
RE: IndexError: list index out of range - BashBedlam - Jan-24-2021 In my system, this code works as expected. Can you post all of the code that is creating the error? orders_file = open('orders.txt') orders_raw = orders_file.read() orders_file.close () orders = orders_raw.splitlines() def get_customer_orders(orders,customer_id): orders_filtered = [] for i in orders: if int(i.split(',')[2])==customer_id: orders_filtered.append(i) return orders_filtered print (get_customer_orders (orders, 256))
RE: IndexError: list index out of range - buran - Jan-24-2021 check your data. is it possible that you have blank/incomplete lines? your code works for sample data |