title: Writing to the end of specific line in python
body: <p>I have a text file that contains key value pairs separated by a tab like this:</p>
  
  <pre><code>KEY\tVALUE
  </code></pre>
  
  <p>I have opened this file in append mode(a+) so I can both read and write. Now it may happen that a particular key has more than 1 value. For that I want to be able to go to that particular key and write the next value beside original one separated by a some delimiter(or ,).
  Here is what I wish to do:</p>
  
  <pre><code>import io
  ft = io.open("test.txt",'a+')
  ft.seek(0)
  for line in ft:
      if (line.split('\t')[0] == "querykey"):
          ft.write(unicode("nextvalue"));#Write the another key value beside the original one
  </code></pre>
  
  <p>Now there are two problems with it:</p>
  
  <ol>
  <li>I will iterate through the file to see on which line the key is present(Is there a faster way?)</li>
  <li>I will write a string to the end of that line.
  <br></li>
  </ol>
  
  <p>I would be grateful if I can get help with the second point.</p>
  
  <p>The write function always writes at the end of file. How should I write to the end of a specific line? I have searched and have not got very clear answers as to how to do that</p>