Usually when we are reading a file we want to do something to the lines other than just printing the whole line. Often we want to find the “interesting lines” and then parse the line to find some interesting part of the line. What if we wanted to print out the day of the week from those lines that start with “From ”.
From email@example.com Sat Jan 5 09:14:16 2008
The split method is very effective when faced with this kind of problem. We can write a small program that looks for lines where the line starts with “From ” and then split those lines and then print out the third word in the line:
fhand = open('mbox-short.txt') for line in fhand: line = line.rstrip() if not line.startswith('From ') : continue words = line.split() print words
We also use the contracted form of the if stat ement where we put the continue on the same line as the if. This contracted form of the if functions the same as if the continue were on the next line and indented.
The program produces the following output:
Later, we will learn increasingly sophisticated techniques for picking the lines to work on and how we pull those lines apart to find the exact bit of information we are looking for.