[Tutor] Making table

Dave Kuhlman dkuhlman at rexx.com
Mon Mar 19 16:31:05 CET 2007


On Mon, Mar 19, 2007 at 01:11:27PM +0100, Per Jr. Greisen wrote:
> Hi,
> I need to generate a table with different x,y,z values and write them to a
> file:
> 10.171  -15.243 -2.558
> 9.837   -14.511 -1.923
> -23.451 -13.870 51.507
> 
> I would like to write to the files as columns
> 10.171   -15.243   -2.558
>   9.837   -14.511   -1.923
> -23.451   -13.870  51.507
>   0.233      0.453    0.111
> 
> so the number are correctly aligned. Does anybody know a function that can
> align the numbers.
> Any advice or help appreciated. Thanks in advance.

Try ljust and rjust.  They are string functions/methods and are
described here: http://docs.python.org/lib/string-methods.html

Something like the following might work for you:

    In [1]: value = 3.45678
    In [2]: ("%0.3f" % value).rjust(10)
    Out[2]: '     3.457'

(using the ipython interactive shell)

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list