On Thu, Aug 25, 2011 at 2:10 PM, Paul Menzel <paulepan...@users.sourceforge.net> wrote: > is there an easy way to also save the indexes of an array (columns, rows > or both) when outputting it to a text file. For saving an array to a > file I only found `savetxt()` [1] which does not seem to have such an > option. Adding indexes manually is doable but I would like to avoid > that. > Is there a way to accomplish that task without reserving the 0th row or > column to store the indexes? > > I want to process these text files to produce graphs and MetaPost’s [2] > graph package needs these indexes. (I know about Matplotlib [3], but I > would like to use MetaPost.) > > > Thanks, > > Paul
Why don't you just write a wrapper for numpy.savetxt that adds the indices? E.g.: In [1]: import numpy as N In [2]: a = N.arange(6,12).reshape((2,3)) In [3]: a Out[3]: array([[ 6, 7, 8], [ 9, 10, 11]]) In [4]: def save_with_indices(filename, output): ...: (rows, cols) = output.shape ...: tmp = N.hstack((N.arange(1,rows+1).reshape((rows,1)), output)) ...: tmp = N.vstack((N.arange(cols+1).reshape((1,cols+1)), tmp)) ...: N.savetxt(filename, tmp, fmt='%8i') ...: In [5]: N.savetxt('noidx.txt', a, fmt='%8i') In [6]: save_with_indices('idx.txt', a) 'noidx.txt' looks like: 6 7 8 9 10 11 'idx.txt' looks like: 0 1 2 3 1 6 7 8 2 9 10 11 ~Brett _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion