#!/usr/bin/python
import os,sys
from os import path
input = open('/home/XXXXXX/ERR001268_1', 'r').read().split('\n')
at = 1
for lines in range(0, len(input)):
line1 = input[lines]
line4 = input[lines+3]
num1 = line1.split(':')[4].split()[0]
num4 = line4.split(':')[4].split()[0]
print num1,num4
at += 1
However I got the error: list index out of range
What’s the problem here?
btw, besides "at +=1", is there any other way to finish this cycle loop?
thx
Lets say
len(input) == 10.range(0, len(input))iterates[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. And when lines > 6 and you’re trying to accessinput[lines+3], it clearly an IndexError, because there is noindex[10],[11]etc .And
line1.split(':')[4]can also raise an IndexError ifline1.count(":") < 4.I didn’t understand the last
atpart, it seems not doing anything, but you can break the loop easily withbreakstatement.Also, naming a variable
inputis a bad idea because it conflicts with builtininputfunction. Andrange(0, len(input)) == range(len(input)), so 0 as range’s first argument is unnecessary.