ziweiguan
ziweiguan

Reputation: 131

IndexError: list index out of range, for re.match

below is part of a script I wrote, in which I have a problem in the if statement. If I want to use re.match('ATOM|MODEL',lines[i]), I got error message. removing the "|MODEL" in re.match, it will work. can anyone give me some hints why this happens? Thank you very much!

new_pdb=open(pdb_out,'w')
i=0
while (i<len(lines)):
    frag=lines[i].split()
# do not know why 'ATOM|MODEL' does not work
    if (re.match('ATOM',lines[i]) and "H" not in frag[2]):
        new_pdb.write(lines[i])
    i=i+1
new_pdb.close()

Below is the error message when I used re.match('ATOM|MODEL',lines[i]):

Traceback (most recent call last): File "non-h-1.py", line 17, in if (re.match('ATOM|MODEL',lines[i]) and "H" not in frag[2]): IndexError: list index out of range

Upvotes: 1

Views: 609

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

At least one of the lines that starts with MODEL contains less than three whitespace-separated items, so frag[2] fails. If you remove |MODEL from the regex, re.match() fails and therefore Python doesn't even try to evaluate frag[2] which is why the error doesn't occur in that situation.

Other than that, you shouldn't be iterating over lines using a while loop - Python is not C. Use

for line in lines:
    frag = line.split()
    if (re.match('ATOM',line) and "H" not in frag[2]):
        new_pdb.write(line)

Upvotes: 1

Related Questions