Formatted numerical output

M

Mark

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!

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

etc.

Thanks,

Mark
 
K

Kent Johnson

Mark said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top