0

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.

Unknown111
  • 15
  • 1
  • 4

1 Answers1

0

Create a new .csv file ('output.csv') and write the new row to the new file after each calculation using csv.writer() (where you have the np.savetxt()). When you have finished, you can close the files and read the new csv file for processing the individual rows that you have created.

Make sure that you open the file and the writer outside the loop (as you did with the read file). Inside the loop write each line to the file. Close the files after you complete processing all the data. If you open the file inside the loop, you will wind up overwriting the file.

CSV File Reading and Writing

sabbahillel
  • 4,357
  • 1
  • 19
  • 36
  • I've updated the code with your advice and the information of csv.writer() on the main post, but I still seem to be getting the same output as before. Only the last array is being saved in the output file. Am I missing an indentation somewhere? – Unknown111 Feb 16 '17 at 16:05
  • @Unknown111 Open the file **outside** the loop. Leave it open until processing is complete. Your change opens the file and closes it for each line in the loop, overwriting each entry with the next. – sabbahillel Feb 16 '17 at 17:36
  • Excellent! Worked great. Another quick question if you wouldn't mind. Is there any way of using csv writer to add in an extra column at the end of the csv file that I generated with the y list above as the values of that the column? – Unknown111 Feb 17 '17 at 13:16
  • @Unknown111 Yes, you can use the dictwriter and dictreader classes will havethe fieldnames parameter set up. That way you can use dictionaries as well. – sabbahillel Feb 17 '17 at 16:21