I opened as CSV file, converted the contents to a numpy array, and then manipulated the arrays. How can I now write this into a new CSV file, 'newtable.csv'?
file_name = "table.csv"
import numpy as np
reader=csv.reader(open("table.csv","rb"),delimiter=',')
x=list(reader)
result=np.array(x).astype('str') # convert to numpy array
I somehow should use
writer = csv.writer(open('newtable.csv', 'wb'), delimiter=',')
However, I did not change the overall structure of the CSV file; I still use the same sizes of columns and rows. (I only changed a few numbers) Do I have to re-write the entire thing?
EDIT: In the case of a numpy array, how would I re-write the entire thing? The problem with np.savetxt("foo.csv", result, delimiter=",")
is that it is not the correct format. One must work with a format specifier somehow.
EDIT2: I should somehow use writerow()
but I'm not sure the best way to do this. I have a numpy array shaped (700,12), so that's 12 "columns" of 700 row entries, and the "title" already exists.
with open("new_file.csv", "w") as toWrite:
writer = csv.writer(toWrite, delimiter=",")
writer.writerow([.....])