-4

I am working with file tabel.txt and I'm having trouble with this. I want to display data from forth column but I am getting

error: list index out of range

Can anyone help me with this Picture of this tabel.txt

f = open("tabel.txt", 'r') 

for line in f:

a=line.split("\t")    
    print(a[3])    
Konstantin
  • 24,271
  • 5
  • 48
  • 65

2 Answers2

0

you can do this:

f = open("tabel.txt", 'r') 
for line in f:
    a=line.split("\t")  
    if len(a)>3:  
        print(a[3])    
    else:
        print a

This way you can see what is the problem in this line. As said in one of the comments, 99% this is the last line.

AvidLearner
  • 4,123
  • 5
  • 35
  • 48
0

As was noted already - most likely you are missing 4th column at some point. The good practice here would be using try..except if you are not sure about your data:

f = open("tabel.txt", 'r') 
i = 0
for line in f:

    a=line.split("\t")   
    try: 
        print(a[3]) 
    except IndexError:
        print("Empty field at line %s" % str(i))    
    i += 1
konart
  • 1,714
  • 1
  • 12
  • 19