title: Python: Compare 2 text files for string and then print context
body: <p>I have 2 text files:</p>
  
  <p>1) cities.txt</p>
  
  <pre><code>San Francisco
  Los Angeles
  Seattle
  Dallas
  </code></pre>
  
  <p>2) master.txt</p>
  
  <pre><code>Atlanta is chill and laid-back.
  I love Los Angeles.
  Coming to Dallas was the right choice.
  New York is so busy!
  San Francisco is fun.
  Moving to Boston soon!
  Go to Seattle in the summer.
  </code></pre>
  
  <p>Trying to get output.txt</p>
  
  <pre><code>&lt;main&gt;&lt;beg&gt;I love&lt;/beg&gt;&lt;key&gt;Los Angeles&lt;/key&gt;&lt;end&gt;&lt;/end&gt;&lt;/main&gt;
  &lt;main&gt;&lt;beg&gt;Coming to&lt;/beg&gt;&lt;key&gt;Dallas&lt;/key&gt;&lt;end&gt;was the right choice&lt;/end&gt;&lt;/main&gt;
  &lt;main&gt;&lt;beg&gt;&lt;/beg&gt;&lt;key&gt;San Francisco&lt;/key&gt;&lt;end&gt;is fun&lt;/end&gt;&lt;/main&gt;
  &lt;main&gt;&lt;beg&gt;Go to&lt;/beg&gt;&lt;key&gt;Seattle&lt;/key&gt;&lt;end&gt;in the summer&lt;/end&gt;&lt;/main&gt;
  </code></pre>
  
  <p>Each entity in cities.txt is the &lt; key>. The master.txt file is much longer, and all lines without the particular city should just be ignored. They're not in order. The output prints out the cities in &lt; key> and &lt; beg> &amp; &lt; end> context (if any).</p>
  
  <p>This is what I have:</p>
  
  <pre><code>with open(master.txt) as f:
      master = f.read()
  working = []
  with open(cities.txt) as f:
      for i in (word.strip() for word in f):
          if i in master:
              print "&lt;key&gt;", i, "&lt;/key&gt;"
  </code></pre>
  
  <p>I know how to check two text files (find 'city' in 'master')... but stuck at the part how I can print the  and  context in master.txt once I find the city!</p>