fhand = open('mbox-short.rtf')
for line in fhand:
words = line.split()
if len(words) == 0:
continue
if words[0] != 'From':
continue
print(words[2])
This example works and takes care of empty lines in the document that I am trying to read. I first came up with the solution below though, which is seemingly a more elegant way to put it:
for line in fhand:
words = line.split()
if words[0] != 'From' or len(words) == 0:
continue
print(words[2])
Yet I get a 'list index out of range' - error in this example, and I don't quite understand why? It seems Python tries the third line subsequently from the left to the right and produces an error if the first condition doesn't work; but isn't that detrimental to the purpose of an 'or'-condition? I'm not yet getting a grasp of the mechanics behind it.