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:
Edit: I've updated the code with the following but still getting the same output to csv file.
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)
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:
[ 9.81 9.09 8.88 8.78 8.62 8.68 8.84]
[ 8.81 8.09 7.88 7.78 7.62 7.68 7.84]
[ 7.81 7.09 6.88 6.78 6.62 6.68 6.84]
[ 6.81 6.09 5.88 5.78 5.62 5.68 5.84]
[ 5.81 5.09 4.88 4.78 4.62 4.68 4.84]
[ 4.81 4.09 3.88 3.78 3.62 3.68 3.84]
[ 3.81 3.09 2.88 2.78 2.62 2.68 2.84]
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?
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:
x=np.array(map(float, (csvFileArray[i])))
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?
Thanks a bunch for the help.