Zapho
Zapho

Reputation: 109

How to use numpy.savetxt with a structured array that contains an array

Say I have a structured array as follows:

a = np.zeros(10,dtype=[('label1',np.int32, 4), ('label2', np.float_)])

And I try to save this as a csv file:

np.savetxt('output.csv', a, fmt='%d,%d,%d,%d,%f')

Python will generate this error:

ValueError: fmt has wrong number of % formats:  %d,%d,%d,%d,%f

I haven't been able to create a fmt string that would suit my output. %s won't work for me here as I need the ability specify the format of the float.

Is it possible to create a csv file from this type of structured array?

Upvotes: 4

Views: 2216

Answers (1)

hpaulj
hpaulj

Reputation: 231540

In essence what savetxt does is:

for row in arr:
    print(fmt % tuple(row))

So the fmt has to work with a row, or in this case, an element of your array.

In [330]: a = np.zeros(10,dtype=[('label1',np.int32, 4), ('label2', np.float_)])
     ...: 
In [331]: a
Out[331]: 
array([([0, 0, 0, 0],  0.), ([0, 0, 0, 0],  0.), ([0, 0, 0, 0],  0.),
       ([0, 0, 0, 0],  0.), ([0, 0, 0, 0],  0.), ([0, 0, 0, 0],  0.),
       ([0, 0, 0, 0],  0.), ([0, 0, 0, 0],  0.), ([0, 0, 0, 0],  0.),
       ([0, 0, 0, 0],  0.)], 
      dtype=[('label1', '<i4', (4,)), ('label2', '<f8')])
In [332]: a[0]
Out[332]: ([0, 0, 0, 0],  0.)
In [333]: tuple(a[0])
Out[333]: (array([0, 0, 0, 0]), 0.0)

Nesting the 4 ints in label1 makes it difficult to come up with a format that works. It's a matter of how to Python '%' formatting.

In [334]: '%s, %f'%_
Out[334]: '[0 0 0 0], 0.000000'

If a was 5 fields it would be easier

In [335]: a = np.zeros(10,dtype='i,i,i,i,f')
In [336]: a
Out[336]: 
array([(0, 0, 0, 0,  0.),....
       (0, 0, 0, 0,  0.)], 
      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<i4'), ('f4', '<f4')])
In [337]: tuple(a[0])
Out[337]: (0, 0, 0, 0, 0.0)
In [338]: '%d, %d, %d, %d, %f'%tuple(a[0])
Out[338]: '0, 0, 0, 0, 0.000000'

I think you need to either 'flatten' the structure of your array, or write a custom savetxt. As I indicated savetxt is not fancy. If you can print the array element by element in the format you want, you can write that format to a file.

Upvotes: 1

Related Questions