title: Using dict keys to find a matching line in a file, and once found, appending the corresponding values in the file
body: <p>I have a dict with a few {key, value} pairs. I also have a file with some content. </p>
  
  <p>The file is something like this:</p>
  
  <pre><code>some random text
  
  ...
  
  ...
  
  text-which-matches-a-key
  
  some important lines
  
  ...
  
  ...
  
  some other important text until end of file
  </code></pre>
  
  <p>What I want is, to search/iterate through the file until a line matches a key of the dict, the append the corresponding value before/after <code>some important lines</code></p>
  
  <p>What I've tried to do is this:</p>
  
  <pre><code>with open('file', 'a+') as f:
      for key in a:
          if f.readlines() == key:
              f.writelines(a[key])
  f.close()
  </code></pre>
  
  <p>where a is a dict, with many key,value pairs.</p>
  
  <p>I'd be happy if the results are something like:</p>
  
  <pre><code>some random text
  
  ...
  
  ...
  
  text-which-matches-a-key
  
  some important lines
  
  value corresponding to the key
  
  ...
  
  ...
  
  some other important text until end of file
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>some random text
  
  ...
  
  ...
  
  text-which-matches-a-key
  
  value corresponding to the key
  
  some important lines
  
  ...
  
  ...
  
  some other important text until end of file
  </code></pre>
  
  <p>Any help is appreciated.</p>
  
  <p>P.S: Using Python 2.7, on PyCharm, on Windows 10.</p>