[Numpy-discussion] printing structured arrays

Bruce Schultz bruce.schultz@gmail....
Tue Mar 9 18:09:34 CST 2010


On Sat, Mar 6, 2010 at 8:35 AM, Gökhan Sever <gokhansever@gmail.com> wrote:
>
>
> On Fri, Mar 5, 2010 at 8:00 AM, Bruce Schultz <bruce.schultz@gmail.com>
> wrote:
>>
>> Output is:
>> ### ndarray
>> [[ 1.   2. ]
>>  [ 3.   4.1]]
>> ### structured array
>> [(1.0, 2.0) (3.0, 4.0999999999999996)]
>>
>>
>> Thanks
>> Bruce
>>
>
> I still couldn't figure out how floating point numbers look nicely on screen
> in cases like yours (i.e., trying numpy.array2string()) but you can make
> sure by using numpy.savetxt("file", array, fmt="%.1f") you will always have
> specified precision in the written file.

Using numpy.array2string() gives the same format as the output above.

Using numpy.savetxt() creates the same nicely formatted file
containing the lines below for both structured and unstructured
arrays.
1.0 2.0
3.0 4.1

But I was mainly curious about this because I just want to quickly
dump data out to the console for debugging, and the unstructured
format is obviously much easier to read. It seems like from other
discussion in the thread that the quick solution is to convert back to
a unstructured array with something like view((float, 2)), but that
seems a bit clumsy.

Cheers
Bruce


More information about the NumPy-Discussion mailing list