title: How to replace the string of the file if matches
body: <p>I have 2 inputs and one file.txt</p>
  
  <p>file.txt have below lines</p>
  
  <pre><code>A1|books|cloths|
  A2|color|pencil|
  A3|ball|
  </code></pre>
  
  <p>input1,input2 </p>
  
  <ul>
  <li><p>input1 has to select which line has to add  (A1,A2 or A3)</p></li>
  <li><p>input2 has to select what string has to added</p></li>
  </ul>
  
  <p>input1=A2, input2=|pen|</p>
  
  <p>then my expected file.txt is </p>
  
  <pre><code>A1|books|cloths|
  A2|color|pen|
  A3|ball|
  </code></pre>
  
  <p><code>notice that pencil is replace with pen</code> </p>
  
  <p>Code is below which will add at the last part of the file</p>
  
  <pre><code>def func(filename,a,b):
      txt1,txt2="",""
      with open(filename,'r') as f:
          txt1 =f.readline().strip()
          while(txt1):
              if a==txt1[:len(a)]:
                  txt1+=b
              txt2+=txt1+'\n'
              txt1=f.readline().strip()
      with open(filename,'w') as f:
          f.write(txt2)
  
  func("file.txt","A2","pen")
  </code></pre>