您在這裡

Parsing lines

23 二月, 2015 - 17:20

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 stephen.marquard@uct.ac.za 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[2]

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:

SatFriFriFri...

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.