title: How to save two-column array in a file in Python
body: <p>I have this code (see <a href="https://stackoverflow.com/questions/28941924/how-to-add-new-column-to-output-file-in-python/28943241?noredirect=1#comment46142245_28943241">this Thread</a>) for saving a two-column array in the file. The thing is that I need to call this function N times:</p>
  
  <pre><code>   def save(self):
          n=self.n
          with open("test.csv","a") as f:
              f.write("name\tnum\n")
              for k, v in tripo.items():
                  if v:
                      f.write(n+"\t")
                      f.write("{}\n".format(k.split(".")[0]))
                      for s in v:
                          f.write(n+"\t")
                          f.write("\n".join([s.split(".")[0]])+"\n")
  </code></pre>
  
  <p>This is the sample content of <code>tripo</code> for n=1:</p>
  
  <pre><code>{
  '1.txt': [], 
  '2.txt': [], 
  '5.txt': [], 
  '4.txt': ['3.txt','6.txt'],
  '7.txt': ['8.txt']
  }
  </code></pre>
  
  <p>This is the expected output for n=1...N:</p>
  
  <pre><code>name num
  1  4
  1  3
  1  6
  1  7
  1  8
  ...
  N 3
  N 6
  N ...
  </code></pre>
  
  <p>However, the above-given code puts some values in the same column.</p>
  
  <p>UPDATE:
  For instance, if I have this string <code>'170.txt': ['46.txt','58.txt','86.txt']</code>, then I receive this result:</p>
  
  <pre><code>1   1   1   1   170
  46
  58
  86
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>1  170
  1  46
  1  58
  1  86
  </code></pre>