[Numpy-discussion] Printing formatted numerical values

Dave Hirschfeld dave.hirschfeld at gmail.com
Mon Nov 15 10:44:11 EST 2010


 <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






More information about the NumPy-Discussion mailing list