title: Writing each line in loop to rows in csv file
body: <p>I'm still in the process of learning python, so please bear with me if this is simple. So I have this piece of code that imports a row from a csv file as a numpy array and subtracts each element from a list from that array. Those resulting arrays are then saved back as rows in a csv file:</p>
  
  <p>Edit: I've updated the code with the following but still getting the same output to csv file.</p>
  
  <pre><code>from pylab import *
  import numpy as np
  import matplotlib.pyplot as plt
  import csv
  from operator import sub
  
  csvfile = open('blackbody.csv')
  csvFileArray = []
  for row in csv.reader(csvfile):
      csvFileArray.append(row)
  x=np.array(map(float, (csvFileArray[1])))
  x_1=np.delete(x, 0)
  y=np.array([1,2,3,4,5,6,7])
  for i in range(0,7):
      z = x_1 - y[i]
      print z
      with open('mags.csv', 'wb') as csvfile:
          file = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
          file.writerow(z)
  </code></pre>
  
  <p>This gives me the following output but only saves the last array as a row in the csv file since the previous ones have been overwritten:</p>
  
  <p>[ 9.81  9.09  8.88  8.78  8.62  8.68  8.84]</p>
  
  <p>[ 8.81  8.09  7.88  7.78  7.62  7.68  7.84]</p>
  
  <p>[ 7.81  7.09  6.88  6.78  6.62  6.68  6.84]</p>
  
  <p>[ 6.81  6.09  5.88  5.78  5.62  5.68  5.84]</p>
  
  <p>[ 5.81  5.09  4.88  4.78  4.62  4.68  4.84]</p>
  
  <p>[ 4.81  4.09  3.88  3.78  3.62  3.68  3.84]</p>
  
  <p>[ 3.81  3.09  2.88  2.78  2.62  2.68  2.84]</p>
  
  <p>But what I want is to essentially save all of the above as rows in a csv file. How would you be able to do that?</p>
  
  <p>Also, if I then wanted to repeat the above process for the next 4 rows in my original csv file as a loop, so essentially change 1 to i (where i=1/2/3/4/5) in the above code:</p>
  
  <pre><code>x=np.array(map(float, (csvFileArray[i])))
  </code></pre>
  
  <p>What would be an easy way to save the outputs from all of those rows on to the same csv file after the output of the first row that we previously saved? </p>
  
  <p>Thanks a bunch for the help.</p>