Formatted numerical output

Kent Johnson kent3737 at yahoo.com
Sat Nov 27 22:43:29 EST 2004


Mark wrote:
> How do I output formatted numerical data to an external (text) file. At 
> a minimum I want to specify the number of decimal places to print. 
> Ideally I'd like the type of control the fortran FORMAT statement 
> offers. I'm sure this is easy, but I can't seem to find the answer!

Use string formatting operations
http://docs.python.org/lib/typesseq-strings.html

> 
> I want an output file to look like this:
> 
> 0.1   0.8575
> 0.2  12.0900
> 0.4 345.0000
> 0.5   9.0002

Something like

s = "%3.1f %8.4f\n" % (x, y)
out.write(s)


Kent



More information about the Python-list mailing list