1

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)
3
  • are those list of numpy array? Commented Nov 6, 2018 at 9:14
  • yes. it is a nD array Commented Nov 6, 2018 at 9:15
  • 1
    In case it isn't clear from the answers, np.savetxt is designed to write a 2d array. Internally it is quite simple. It makes input an array if it isn't already, and then just iterates over the rows, doing a python formatted write of the tuple(row). So if your list makes a nice 2d numeric array it should work fine. Commented Nov 6, 2018 at 16:57

3 Answers 3

2

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"
Sign up to request clarification or add additional context in comments.

Comments

2

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=',')

1 Comment

@timgeb that can be done, I will update the code shortly
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.