Read out definitions of a text file based dictionary
Question: I'm trying to write a Python function that takes as an input a text file based
dictionary, for example Webster's free dictionary. The function
"webster_definition" will then search through the text file and print the
definition for a specific word, e.g. "Canada".

Here is what I've got so far:

    
    
    import re
    import sys
    
    def webster_definition(word):
        word = word.upper()
        output = ""
        doc = open("webster.txt",'r')           
    
        for line in doc:     
            if re.match(word,line):
                print line
    
        return output
    
    print webster_definition("Canada")
    

This will output the word I looked for. But the definition starts three lines
later with "Defn:" and is of variable length, e.g.:

    
    
    CANADA
    Can"a*da, n.
    
    Defn: A British province in North America, giving its name to various
    plants and animals. Canada balsam. See under Balsam.
     -- Canada goose. (Zoöl.) See Whisky Jack.
     -- Canada lynx. (Zoöl.) See Lynx.
     -- Canada porcupine (Zoöl.) See Porcupine, and Urson.
     -- Canada rice (Bot.) See under Rick.
     -- Canada robin (Zoöl.), the cedar bird.
    

The desired output should look like:

    
    
    CANADA
    Defn: A British province in North America, giving its name to various
    plants and animals. Canada balsam. See under Balsam.
     -- Canada goose. (Zoöl.) See Whisky Jack.
     -- Canada lynx. (Zoöl.) See Lynx.
     -- Canada porcupine (Zoöl.) See Porcupine, and Urson.
     -- Canada rice (Bot.) See under Rick.
     -- Canada robin (Zoöl.), the cedar bird.
    

Can anyone help me with the output of the definition?


Answer: in file:

    
    
    CANADA
    Can"a*da, n.
    
    Defn: A British province in North America, giving its name to various
    plants and animals. Canada balsam. See under Balsam.
     -- Canada goose. (Zoöl.) See Whisky Jack.
     -- Canada lynx. (Zoöl.) See Lynx.
     -- Canada porcupine (Zoöl.) See Porcupine, and Urson.
     -- Canada rice (Bot.) See under Rick.
     -- Canada robin (Zoöl.), the cedar bird.
    
     ANOTHER DEFENITION
     another defenition
    
     Defn.. some words
     more words
     ......
    
    
    with open('webster_file', 'r') as f:
         # read into a string.
         data = f.read()
    
    # uppercase word to search for
    word = 'canada'.upper()
    # search for empty line and the get everything non-greedy up to the 
    # another empty line.
    pattern = '^' + word + '.*?\n^$\n.*?^$'
    
    mo = re.search(pattern,data,re.M|re.DOTALL)
    
    if mo:
        print(mo.group(0))
    
    
    CANADA
    Can"a*da, n.
    
    Defn: A British province in North America, giving its name to various
    plants and animals. Canada balsam. See under Balsam.
     -- Canada goose. (Zoöl.) See Whisky Jack.
     -- Canada lynx. (Zoöl.) See Lynx.
     -- Canada porcupine (Zoöl.) See Porcupine, and Urson.
     -- Canada rice (Bot.) See under Rick.
     -- Canada robin (Zoöl.), the cedar bird
    

