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

Paul Menzel paulepanter at users.sourceforge.net
Thu Aug 25 15:10:34 EDT 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.python.org/pipermail/numpy-discussion/attachments/20110825/1f7ba2a6/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110825/1f7ba2a6/attachment.sig>


More information about the NumPy-Discussion mailing list