Click here to Skip to main content
15,507,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am doing a code that check line in file called CSG.txt and compare this line to a line in another file called ODR.txt

whenever I am trying to copy new data to CSG file using copy and paste from windows, code fails with error (especially after code run for first time, second time fails)

"
Quote:
Traceback (most recent call last):
File "C:\Users\ismohamm\Desktop\py4e\first.py", line 75, in <module>
soc_email()
File "C:\Users\ismohamm\Desktop\py4e\first.py", line 17, in soc_email
print(line[0],line[1],line[2],line[3],line[5],line[7],line[8])
IndexError: list index out of range


I need to delete CSG.txt and create new one to avoid that error

What I have tried:

#this code to list sites for SOC email
#This part of code prints out list of CSGs
odr=open('ODR.txt')
slist=list()
crlist=list()
linecounter=0
for line in odr:
    line=line.rstrip()
    csg=open('CSG.txt')
    for site in csg:
        site=site.strip()
        if site in line and len(line)>1:
            line=line.split()
            linecounter=linecounter+1
            print(line[0],line[1],line[2],line[3],line[5],line[7],line[8])
            slist.append(site)
            if len(crlist)<1:
                crlist.append(line[1])
            elif line[1] not in crlist:
                crlist.append(line[1])
    csg.close()
odr.close()
#This part of code prints out list of uplink routers.
odr=open('ODR.txt')
loc=0
last=None
for l in odr:
    l=l.strip()
    for cr in crlist:
        if cr in l and 'CSG' not in l and 'SAR' not in l:
            loc=l.rfind('00:00')
            linecounter=linecounter+1
            last=l[loc+6:].replace(" ","")
            l=l.split()
            print(l[0],l[1],l[2],l[3],l[5],l[7],last)
odr.close()
#This part of code prints out CSG short name and no of lines
c1=None
c2=None
for s in slist:
    s=s.split('-')
    c1=s[0]
    c2=s[1]
    print(c1+c2)
print('No of Lines is:',linecounter)
Posted
Updated 13-Aug-22 22:28pm

"Index out of range" means exactly what it says: the collection you are indexing into does not have enough elements to cover the index value you are using.
In Python, indexes run from 0 to N - 1, where N is the number of values in the collection. So it you have a list containing three strings, the only valid indexes are 0, 1, and 2 - any other number will throw an "out of range" exception.

We don't have your data, which means we can't run your code under the same circumstances you do.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
You should avoid hardcoded indexes when you do not know how many items there may be.
Python
line=line.split()
linecounter=linecounter+1
print(line[0],line[1],line[2],line[3],line[5],line[7],line[8])

When you use the split function, you cannot always be certain how many fields it is split into. So you need a better method to print all the items.
Python
fields = line.split()
linecounter=linecounter+1
print(len(fields)) # for debugging show how many items
for index in range(len(fields)):
    # to exclude any field check the index value here
    print(fields[index], '', end='') # print each item from the split line
print('') # don't forget a newline after the last item
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900