[Numpy-discussion] Printing formatted numerical values

Jonathan Hilmer jkhilmer@gmail....
Mon Nov 15 10:18:56 CST 2010


Is there a convention for dealing with NaN and Inf?  I've found that
trusting the default behavior is a very bad idea:

-----------------------------------
from numpy import *
x = zeros((5,7))
x[:,3:] = nan
x[:,-1] = inf
savetxt('problem_array.txt',x,delimiter='\t')
x2 = loadtxt('problem_array.txt')
print(x.shape)  # (5, 7)
print(x2.shape) # (5, 4)
print(x2[0,:])
print(open('problem_array.txt').readline().strip().split('\t'))
-----------------------------------

On my system the loaded array is reduced in size relative to the saved
array.  Does savetxt need some conditionals to deal with special
values like these?

Jonathan



On Mon, Nov 15, 2010 at 8:44 AM, Dave Hirschfeld
<dave.hirschfeld@gmail.com> wrote:
>  <pv+numpy <at> math.duke.edu> writes:
>
>>
>> Hi, what is the best way to print (to a file or to stdout) formatted
>> numerical values? Analogously to C's printf("%d %g",x,y) etc?
>>
>
> For stdout you can simply do:
>
> In [26]: w, x, y, z = np.randint(0,100,4)
>
> In [27]: type(w)
> Out[27]: <type 'numpy.int32'>
>
> In [28]: print("%f %g %e %d" % (w,x,y,z,))
> 68.000000 64 1.300000e+01 57
>
> In [29]: w, x, y, z
> Out[29]: (68, 64, 13, 57)
>
> For a file I would use np.savetxt
>
> HTH,
> Dave
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list