How do I fix the "Index out of range" error in Python?

Thobekani Kubeka

I am trying to create a record from a list of 6 items. The error tells me that rec[1] is out of range.

pay = open("paymast.txt","r")
sal = open("saltyp.txt","w")

heading = pay.readline()

for rec in pay:

    rec = rec.split(",")
    
    id = rec[0]
    name = rec[1]
    gender = rec[2]
    code = rec[3]
    grade = rec[4]
    salary = rec[5]
    salary = salary.strip('\n')
    
    record = id+","+name+","+gender+","+code+","+grade+","+salary

    if int(salary) < 1500 and gender == "M":
        
        sal.write(record)

pay.close()
sal.close()
CcmU

Try to check your file paymast.txt. I've tried with an array of string, formatted as "id,name,..." and the split works fine, the only time that it raises an IndexError is when the input is bad formatted.

For example:

pay = ["1", "0,ted,male,4,23,1440\n",
       "2,katie,female,1,2,240\n"]
# The first index should raise the error

for rec in pay:
    rec = rec.split(",")

    id = rec[0]
    name = rec[1]
    gender = rec[2]
    code = rec[3]
    grade = rec[4]
    salary = rec[5]
    salary = salary.strip('\n')

    record = id+","+name+","+gender+","+code+","+grade+","+salary
    print(record)

Raised error:

Traceback (most recent call last):
  File "so.py", line 9, in <module>
    name = rec[1]
IndexError: list index out of range

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I fix a ' list index out of range' error?

How do I fix Index Out of Range in Python?

How can I fix this error in python 3.4 tkinter "Index Error: list index out of range"

how can I fix this error in this code? index out of range

How can i fix this error it says the list index is out of range?

How I can fix this error IndexError: string index out of range

How do I fix index out of range when index is in range

How to fix "IndexError: string index out of range" error in python

How do you fix 'IndexError: list index out of range' in Python

How can I fix IndexError: list index out of range in Python?

How do I resolve list index out of range error?

golang :how do I handle index out of range error?

How do I handle an error ''list index out of range" as an exception?

How to fix this list index out of range error

How to fix 'String index out of range' error

How to fix the error "index out of range"

how to fix tuple error, index out of range

How to Fix "List Index out of range" in python?

How to fix Index out of range in python

How to fix : "list index out of range ..." in Python?

How to solve index out of range error in python?

Python Web Scraping error - Reading from JSON- IndexError: list index out of range - how do I ignore

How to fix "IndexError: list index out of range" error of p2p chat application on Python?

list index out of range Error Pygame How To Fix?

how to fix an index out of range error and instead print a statement

How to fix 'list index out of range' error in ZigZag problem?

How to fix Thread 1: Fatal error: Index out of range

How can I fix tuple index out of range

Python List of Lists why do I get this Error: IndexError: list index out of range?