[Numpy-discussion] How to output array with indexes to a text file?

Paul Menzel paulepanter@users.sourceforge....
Thu Aug 25 14:10:34 CDT 2011


Dear NumPy folks,


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.

        --- minimal example (also attached) ---
        from numpy import *
        
        a = zeros([2, 3], int)
        print(a)
        
        savetxt("/tmp/test1.txt", a, fmt='%8i')
        
        # Work around for adding the indexes for the columns.
        a[0] = range(3)
        print(a)
        
        savetxt("/tmp/test2.txt", a, fmt='%8i')
        --- minimal example ---

The output is the following.

        $ python output-array.py 
        [[0 0 0]
         [0 0 0]]
        [[0 1 2]
         [0 0 0]]
        $ more /tmp/test*
        ::::::::::::::
        /tmp/test1.txt
        ::::::::::::::
               0        0        0
               0        0        0
        ::::::::::::::
        /tmp/test2.txt
        ::::::::::::::
               0        1        2
               0        0        0

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


[1] http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
[2] http://wiki.contextgarden.net/MetaPost
[3] http://matplotlib.sourceforge.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: output-array.py
Type: text/x-python
Size: 209 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110825/1f7ba2a6/attachment.py 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110825/1f7ba2a6/attachment.bin 


More information about the NumPy-Discussion mailing list