Try to write a numpy array (generated via a loop) in csv format

advertisements

I have a code that generates a 1-D numpy array in each iteration. I want the arrays to get appended to the end of a CSV file so that I can read all data from Excel. I am currently trying the following method:

for loop in range(0,10):
    # The following part generates the array
    Array1 = numpy.array([4.3])
    Array2 = numpy.array([10.2])
    Array3 = numpy.concatenate((Array1,Array2),axis=0)

    # The following part tries to generate a CSV writable array. But it fails :S
    if (loop == 0):
        ArrayMain = Array3
    else:
        # Trying to append the new array with the previous array
        ArrayMain = numpy.asarray(ArrayMain,Array3)  

# Trying to write the array into a .txt. file in .csv format
numpy.savetxt("ArrayMain.csv", ArrayMain, delimiter=",",fmt='%.3f')

This code is giving errors. Any idea how I can rectify it?


I think you need to use append function to append new array with previous array, asarray function converts input to array.

    else:
        # Trying to append the new array with the previous array
        ArrayMain = numpy.append(ArrayMain,Array3)