title: String replace in a text file
body: <p>i have one text file (.txt),with multiple scenario i want to replace the value which is in &quot;&lt; &gt;&quot; with | under the Examples.</p>
  <p>When user enters the username &quot;&lt; username &gt;&quot; and password  &quot;&lt; password &gt;&quot;</p>
  <p>When user enters the phonenumber &quot;&lt; phone &gt;&quot;</p>
  <p>When user enters pincode &quot;&lt; code &gt;&quot;</p>
  <p>when user enters filghtnumber &quot;&lt; flight &gt;&quot;</p>
  <p>Examples:</p>
  <p>| username | password |phone|code| flight|</p>
  <p>| john@123 | 789_456 |4556789|33456|ACH003|</p>
  <p>When user enters the username &quot;&lt; username &gt;&quot; and password &quot;&lt; password &gt;&quot;</p>
  <p>When user enters the phonenumber &quot;&lt; phone &gt;&quot;</p>
  <p>When user enters pincode &quot;&lt; code &gt;&quot;</p>
  <p>when user enters filghtnumber &quot;&lt; flight &gt;&quot;</p>
  <p>Examples:</p>
  <p>| username | password |phone|code| flight|</p>
  <p>|David@2356 | two_003 |67904578|0337654|IG607|</p>
  <p><strong>i want my data looks like this</strong></p>
  <p>When user enters the username &quot;john@123&quot; and password  &quot;789_456 &quot;</p>
  <p>When user enters the phonenumber &quot;4556789&quot;</p>
  <p>When user enters pincode &quot;33456&quot;</p>
  <p>when user enters filghtnumber &quot;ACH003&quot;</p>
  <p>When user enters the username &quot;David@2356&quot; and password &quot;two_003&quot;</p>
  <p>When user enters the phonenumber &quot;67904578&quot;</p>
  <p>When user enters pincode &quot;0337654&quot;</p>
  <p>when user enters filghtnumber &quot;IG607&quot;</p>
  <p>i tried below code, but it is working only one section of sceanrio , if the same value repeating it is not working. i am getting key error.</p>
  <pre><code>import re
  
  text = []
  data = []
  List1=[]
  
  with open(r'C:\user\public\test.txt') as fp:
  
      line = ''
  
      for line in fp:
  
          if line.startswith('Examples'):
   
              break
  
          text.append(line)
  
      text = ''.join(text)
  
      headers = re.split('\s*\|\s*', fp.readline())[1:-1]
  
      for line in fp:
  
          values = re.split('\s*\|\s*', line)[1:-1]
  
          data.append(dict(zip(headers, values)))
  
  
  for d in data:
      
       List1.append(re.sub(r'\&lt;(?P&lt;key&gt;[^&gt;]*)\&gt;', lambda x: d[x.group('key')], text))
  </code></pre>