title: Saving table in a .txt file
body: <p>I was wondering if you could help me with a rather simple bit of code that I got stuck on.</p>
  <p>I have a table in .txt that looked like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>FileName</th>
  <th>predOGT (C)</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>CTOTU-1_1.fasta</td>
  <td>31.30</td>
  </tr>
  <tr>
  <td>CTOTU-2_1.fasta</td>
  <td>21.39</td>
  </tr>
  <tr>
  <td>CTOTU-0_1.fasta</td>
  <td>11.31</td>
  </tr>
  <tr>
  <td>CTOTU-2_5.fasta</td>
  <td>42.32</td>
  </tr>
  <tr>
  <td>CTOTU-2_2.fasta</td>
  <td>34.33</td>
  </tr>
  <tr>
  <td>CTOTU-1_2.fasta</td>
  <td>35.34</td>
  </tr>
  <tr>
  <td>CTOTU-0_3.fasta</td>
  <td>23.35</td>
  </tr>
  <tr>
  <td>CTOTU-1_3.fasta</td>
  <td>64.36</td>
  </tr>
  <tr>
  <td>CTOTU-2_4.fasta</td>
  <td>33.37</td>
  </tr>
  <tr>
  <td>CTOTU-2_3.fasta</td>
  <td>27.38</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Which I have successfully changed to look like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>OldFile</th>
  <th>NewFile</th>
  <th>predOGT_(C)</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>CTOTU-1</td>
  <td>1</td>
  <td>31.30</td>
  </tr>
  <tr>
  <td>CTOTU-2</td>
  <td>1</td>
  <td>21.39</td>
  </tr>
  <tr>
  <td>CTOTU-0</td>
  <td>1</td>
  <td>11.31</td>
  </tr>
  <tr>
  <td>CTOTU-2</td>
  <td>5</td>
  <td>42.32</td>
  </tr>
  <tr>
  <td>CTOTU-2</td>
  <td>2</td>
  <td>34.33</td>
  </tr>
  <tr>
  <td>CTOTU-1</td>
  <td>2</td>
  <td>35.34</td>
  </tr>
  <tr>
  <td>CTOTU-0</td>
  <td>3</td>
  <td>23.35</td>
  </tr>
  <tr>
  <td>CTOTU-1</td>
  <td>3</td>
  <td>64.36</td>
  </tr>
  <tr>
  <td>CTOTU-2</td>
  <td>4</td>
  <td>33.37</td>
  </tr>
  <tr>
  <td>CTOTU-2</td>
  <td>3</td>
  <td>27.38</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Now I would like to save this new table to a new .txt table</p>
  <p>Below is my code which so far allows only the last row to be saved in the <code>new_file.txt</code>.</p>
  <p>Could you tell me what mistake I am doing?</p>
  <pre><code>filepath = 'original_file.txt'
  new_filepath = 'new_file.txt'
  
  with open(filepath, 'r+') as f:
      for line in f:
          new_line = line.strip()
          new_line = new_line.replace('_','\t').replace('FileName','OldFile\tNewFile').replace('predOGT (C)','predOGT_(C)').replace('.fasta','').replace('CTOTU-','')
          print(new_line)
          with  open(new_filepath, &quot;w&quot;) as n:
              new = n.writelines(new_line)
  </code></pre>