[Numpy-discussion] Printing formatted numerical values

pv+numpy@math.duk... pv+numpy@math.duk...
Mon Nov 15 08:32:34 CST 2010


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?

Numpy Documentation only discusses input *from* a file, or output of 
entire arrays. (np.savetxt()) I just want tab or space-delimited output of 
selected formatted values.

In the absence of numpy documentation on this matter, I tried to follow 
python documentation and find errors. Below is ipython 
-pylab transcript, which apparently complains that an int32 variable is 
an object of type 'str'. How should I understand this? Does python not 
understand that numpy.int32 is an integer?

Thank you!


In [2] import numpy as np
In [3]: w = np.arange (1,5,dtype=np.int32).reshape((2,2))
In [4]: w Out[4]: array([[1, 2],

     [3, 4]], dtype=int32)

In [5]: w[0,0]
Out[5]: 1

In [6]: w[0,0].class Out[6]: <type 'numpy.int32'>

In [7]: print('{0:2d}'.format(w[0,0]))

ValueError? Traceback (most recent call last)

/home/p/o/dev/thesis/python/wavelet/<ipython console> in <module>()

ValueError?: Unknown format code 'd' for object of type 'str'

  --


More information about the NumPy-Discussion mailing list