Skip to main content
Engineering LibreTexts

8.10: Parsing lines

[ "article:topic", "authorname:severancec", "python (language)", "license:ccbyncsa" ]
  • Page ID
    3141
  • 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.zaSatJan 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 ", 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])
    
    # Code: http://www.py4e.com/code3/search5.py

    Here we also use the contracted form of the if statement 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:

    Sat
    Fri
    Fri
    Fri
        ...

    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.