title: Creating a TXT file and seeking a position in Python
body: <p>I have given the following variables:</p>
  <pre><code>signal1 = 'speed'
  bins1 = [0, 10, 20, 30, 40]
  
  signal2 = 'rpm'
  bins2 = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]
  
  hist_result = [ [1, 4, 5, 12], 
                  [-5, 8, 9, 0],
                  [-6, 7, 11, 19],
                  [1, 4, 5, 12], 
                  [-5, 8, 9, 0],
                  [-6, 7, 11, 19],
                  [1, 4, 5, 12], 
                  [-5, 8, 9, 0],
                  [-6, 7, 11, 19],
                ]
  </code></pre>
  <p>I want to create a .TXT file which would look like this with tab separated values:</p>
  <pre><code>
                  speed&gt;= 0   10  20  30
                  speed&lt;  10  20  30  40
          rpm&gt;=   rpm&lt;    
          0   500         1   4   5   12   
          500 1000        5   8   9   0
          1000    1500    6   7   11  19
          1500    2000    1   4   5   12
          2000    2500    -5  8   9   0
          2500    3000    -6  7   11  19
          3000    3500    1   4   5   12
          3500    4000    -5  8   9   0
          4000    4500    -6  7   11  19
  </code></pre>
  <p>I have written the following code:</p>
  <pre><code>#!/usr/bin/env python3
  
  import os
  from datetime import datetime
  import time
  
  signal1 = 'speed'
  bins1 = [0, 10, 20, 30, 40]
  
  signal2 = 'rpm'
  bins2 = [0, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500]
  
  hist_result = [ [1, 4, 5, 12], 
                  [-5, 8, 9, 0],
                  [-6, 7, 11, 19],
                  [1, 4, 5, 12], 
                  [-5, 8, 9, 0],
                  [-6, 7, 11, 19],
                  [1, 4, 5, 12], 
                  [-5, 8, 9, 0],
                  [-6, 7, 11, 19],
                ]
  
  filename = f&quot;{datetime.now().strftime('%Y%m%d_%H%M%S')}_{signal1}_results.TXT&quot;
  
  with open(filename, 'w') as f:
     
      # write the bin1 range
      f.write('\n\n\n')
      f.write('\t\t\t\t')
      f.write(signal1 + '&gt;=')
  
      for bin in bins1[:-1]:
          f.write('\t' + str(bin))
      f.write('\n')
      f.write('\t\t\t\t')
  
      f.write(signal1 + '&lt;')
      for bin in bins1[1:]:  
          f.write('\t' + str(bin))
      f.write('\n')
      
      # write the bin2 range
      f.write('\t\t')
      f.write(signal2 + '&gt;=' + '\t' + signal2 + '&lt;' + '\n')
      f.write('\t\t') 
  
      # store the cursor position from where hist result will be written line by line
      track_cursor_pos = []            
  
      curr = bins2[0]
      for next in bins2[1:]:
          f.write(str(curr) + '\t' + str(next)) 
          track_cursor_pos.append(f.tell()) 
          f.write('\n\t\t')
          curr = next
      f.write('\n')   
  
      print(track_cursor_pos)  
  
      i = 0
      # Everything is fine until here 
      # Code below doesn't work as expected!?
      for result in hist_result: 
          f.seek(track_cursor_pos[i], os.SEEK_SET)
          for r in result:            
              f.write('\t' + str(r))
          f.write('\n')
          i += 1
  
  </code></pre>
  <p>But, this is producing the TXT file whose contents look like this:</p>
  <pre><code>
                  speed&gt;= 0   10  20  30
                  speed&lt;  10  20  30  40
          rpm&gt;=   rpm&lt;
          0   500 1   4   5   12
  0   -5  8   9   0
  00  -6  7   11  19
      1   4   5   12
  00  -5  8   9   0
  00  -6  7   11  19
      1   4   5   12
  00  -5  8   9   0
  00  -6  7   11  19
  </code></pre>
  <p>I think I am not using the <code>f.seek()</code> properly. Any suggestion would be appreciated. Thanks in advance.</p>