writing a 2D array to csv for each frame of an image

1053 views python
2

I have a getX() which gives a 2D array(shape=(18,2)) as an output for each frame of an image. I want to add this continuously updating array to the csv. I have used the following code. But it is giving me only one row(probably for only one frame).

with open("data.csv",mode ='w') as csvfile:
            wr = csv.writer(csvfile, quoting = csv.QUOTE_ALL,dialect="excel")
            get_val = oneObject.getX(currentFrameIndex)
            for  currentFrameIndex in get_val:
                wr.writerow(humanPos)

answered question

1 Answer

11

Why not accumulate your sub-arrays and then use np.savetxt to write to CSV just once?

arrs = []
for currentFrameIndex in listOfFrameIndices:  # Demonstrative only.
    arrs.append(oneObject.getX(currentFrameIndex))
# Or,
# arrs = np.array([oneObject.getX(c) for c in listOfFrameIndices])

np.savetxt('file.csv', np.array(arrs), delimiter=',')

posted this

Have an answer?

JD

Please login first before posting an answer.