I am trying to print line[4] if there are 4 items, or line [4] and [5] if there are more than 4 items.
def onlinedoc(test):
for line in test:
lines = line.split()
if 'report' in lines:
if lines > [4]: #<---- this is where i need help
doc = lines[4] + lines[5]
else:
doc = lines[4]
return doc
if __name__ == '__main__':
test = open('test_documentation.txt', 'r')
print
onlinedoc(test)
I’m not sure what I’m suppose to put where I have if lines > [4]. I always get IndexError: list index out of range. I have double-checked and the information I want will either be in [4] or [5]. If I copy the lines to a separate text and do it without the if else and just
if 'report' in lines:
host = lines[4] + lines[5]
then it works (on a line with 5).
you should use
if len(lines)> 4