Simplest is to just concatenate the extra data and write with %s
to handle the text
In [97]: I = np.arange(5)
In [98]: I[None]
Out[98]: array([[0, 1, 2, 3, 4]])
In [101]: np.savetxt('test',I[None],delimiter=',', fmt='%e')
In [102]: cat test
0.000000e+00,1.000000e+00,2.000000e+00,3.000000e+00,4.000000e+00
now to add a text column:
In [103]: lbl = np.array([['test']])
In [105]: np.hstack((I[None],lbl))
Out[105]: array([['0', '1', '2', '3', '4', 'test']], dtype='<U21')
This is now all string, so you'll have to write with '%s':
In [106]: np.savetxt('test',_,delimiter=',', fmt='%s')
In [107]: cat test
0,1,2,3,4,test
Or make an object
dtype array, and apply a custom fmt to each column:
In [108]: lbl = np.array([['test']],object)
In [109]: np.hstack((I[None],lbl))
Out[109]: array([[0, 1, 2, 3, 4, 'test']], dtype=object)
In [110]: np.savetxt('test',_,fmt='%d, %e, %e, %e, %e, %s')
In [111]: cat test
0, 1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00, test
I've also recommended making a structured array, though this object dtype might be easier to use.
In [112]: dt = np.dtype('i,f,f,f,f,U10')
In [113]: arr = np.array([tuple(Out[109][0])],dt)
In [114]: arr
Out[114]:
array([(0, 1., 2., 3., 4., 'test')],
dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '<f4'), ('f3', '<f4'), ('f4', '<f4'), ('f5', '<U10')])
In [115]: np.savetxt('test',_,fmt='%d, %e, %e, %e, %e, %s')
In [116]: cat test
0, 1.000000e+00, 2.000000e+00, 3.000000e+00, 4.000000e+00, test
savetxt
iterates on the 1st dimension of the input array, and writes it using your fmt
(or one constructed from your fmt
). If you are familiar enough with Python formatting and file writing, you could do the same - and it would run just as fast.