sam
sam

Reputation: 494

Writing a list of NumPy arrays to csv

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).

[array([178.,  76.]), array([184.,  92.]), array([164.,  90.]), array([154., 116.]), array([160., 126.]), array([204.,  94.]), array([208., 124.]), array([190., 132.]), array([164., 152.])]

I have tried this:

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

Upvotes: 1

Views: 2091

Answers (3)

dalonlobo
dalonlobo

Reputation: 503

You can save the numpy array's directly to csv, you don't need any other module.

import numpy as np
array = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
numpy.savetxt("output.csv", array, delimiter=",")

To save it in single row and quote the output, you can use the ravel like @jpp suggested.

import numpy as np
array = np.asarray([ [1,2,3], [4,5,6], [7,8,9] ])
np.savetxt('output.csv', np.array(array).ravel()[None], fmt='"%s"', delimiter=',')

Upvotes: 2

jpp
jpp

Reputation: 164793

You can use np.ndarray.ravel after converting your list of arrays (data) to a single array. Then use either the csv module or NumPy, depending on the precise output you require.

csv

with open('data.csv', mode='w') as csvfile:
    wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, dialect='excel')
    wr.writerow(np.array(data).ravel())

Output:

"178.0","76.0","184.0","92.0","164.0","90.0","154.0","116.0","160.0","126.0","204.0","94.0","208.0","124.0","190.0","132.0","164.0","152.0"

numpy

To save on one line you need to slice via [None]. In addition, you can specify formatting via the fmt parameter of np.savetxt. If you require text-based storage, this solution should be preferred to save and read arrays via NumPy.

np.savetxt('data.csv', np.array(data).ravel()[None], fmt='%d', delimiter=',')

Output:

178,76,184,92,164,90,154,116,160,126,204,94,208,124,190,132,164,152

Upvotes: 1

timgeb
timgeb

Reputation: 78780

You can un-nest data with itertools.chain.from_iterable and use wr.writerow.

>>> from itertools import chain
>>> 
>>> with open("data.csv", mode='w') as csvfile:
...:      wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, dialect="excel")
...:      wr.writerow(chain.from_iterable(data))                                                                                                                 

Output:

$ cat data.csv
"178.0","76.0","184.0","92.0","164.0","90.0","154.0","116.0","160.0","126.0","204.0","94.0","208.0","124.0","190.0","132.0","164.0","152.0"

Upvotes: 2

Related Questions