title: How to replace all occurrences in a line from reading a file?
body: <p>With the given code, it replaces and does what it should do, the problem is that it doesn't replace multiple in a same line, only the first one it finds, then moves on to next line.</p>
  <p>The data structure looks like this:</p>
  <pre><code>if js.hasItemById(I06N) and js.hasItemById(I064)
      js.getItemById(I088).remove()
      js.getItemById(I061).remove()
      js.addItemById(I06Y)
  </code></pre>
  <p>It replaces everything in the parentheses , but (I064) for example does not get replaced, only the first found occurrence.</p>
  <pre><code>uniques = []
  count = 0
  code = &quot;&quot;
  new_text = &quot;&quot;
  ## --- FINDS THE ITEMS IN BRACKETS, TO REPLACE --- ##
  with open(&quot;nest.txt&quot;, &quot;r+&quot;) as f:
      for line in f:
          open_br = line.find(&quot;(&quot;)
          close_br = line.find(&quot;)&quot;)
  
          in_brackets = line[open_br + 1:close_br]
          
          if in_brackets in uniques:
              code = &quot;ITEM_&quot; + str(uniques.index(in_brackets))
  
          else:
              uniques.append(in_brackets)
              code = &quot;ITEM_&quot; + str(count)
              count += 1
  
          new_text += line.replace(in_brackets, code)
          
  
      f.seek(0)
      f.truncate(0)
      f.write(new_text)
  </code></pre>