0

I'm a beginner programmer writing a Python script which ultimately needs to pull the contents of three columns and write them to a new text file. When testing the script, i keep getting the error: IndexError: list index out of range applying to the third column I need to pull.

My code:

for ip in open("list.txt"):
    with open(ip.strip()+".txt", "a") as ip_file:
        for line in open("data.txt"):
            new_line = line.split(" ")
            if "blocked" in new_line:
                if "src="+ip.strip() in new_line:
                    ip_file.write(", " + new_line[11])
                    ip_file.write(", " + new_line[12])
                    ip_file.write(", " + new_line[14] + "\n")

In a few of the lines in data.txt, there is no 15th column. I have tried a couple ways to avoid the error message, but I still get the error:

if "src="+ip.strip() in new_line:
    ip_file.write(", " + new_line[11])
    ip_file.write(", " + new_line[12])
    if new_line[14] is None:
        pass
    else:
        ip_file.write(", " + new_line[14] + "\n")

and

if "src="+ip.strip() in new_line:
    ip_file.write(", " + new_line[11])
    ip_file.write(", " + new_line[12])
    if not new_line[14]:
        pass
    else:
        ip_file.write(", " + new_line[14] + "\n")

Question: Is there another way to check if the column exists and ignore if does not?

Thanks!

hjames
  • 439
  • 5
  • 12
  • 19

3 Answers3

2

Use try-except:

ip_file.write(", " + new_line[12])
try:
    ip_file.write(", " + new_line[14] + "\n")
except IndexError:
    pass

By doing if new_line[14] is None or if not new_line[14] you're trying to check the value that exists at 14th index, but in python if there's no such index then IndexError is raised.

(In languages like javascript undefined is returned for non-existent indexes but that's not the case with python)

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 5
    It's a common practice in Python, to simply try something instead of checking first, if it is possible/existing (EAFP style ("it's Easier to Ask for Forgiveness than Permission")) – Robert Caspary Aug 22 '13 at 13:46
0

The error is caused by the lookup itself (new_line[14]), so you have to check the length of the row like if len(new_line) > 12. The other approach is handling the IndexError exception -- both are good, and usually the programmer can decide which approach to use. See "Ask forgiveness not permission" - explain

Community
  • 1
  • 1
dnet
  • 1,411
  • 9
  • 19
0

Using try will avoid your exception.

try:
    ip_file.write(", " + new_line[11])
except IndexError:
    pass   
try:
    ip_file.write(", " + new_line[12])
except IndexError:
    pass
try:
    ip_file.write(", " + new_line[14])
except IndexError:
    ip_file.write("\n")

You were getting an index out of bounds exception when you were doing your comparison to see if it existed.

NOTE:
This is probably not what you want to do since each column is likely unique and contains specific information. If column 2 is missing, this code will not behave as you expect. We would need to know what is in the columns to help you with that.

Stephan
  • 16,509
  • 7
  • 35
  • 61