Formatted numerical output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    #1

    Formatted numerical output

    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
  • Kent Johnson

    #2
    Re: Formatted numerical output

    Mark wrote:[color=blue]
    > 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![/color]

    Use string formatting operations

    [color=blue]
    >
    > 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[/color]

    Something like

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


    Kent

    Comment

    • Mark

      #3
      Re: Formatted numerical output

      Kent Johnson wrote:[color=blue]
      > Mark wrote:
      >[color=green]
      >> How do I output formatted numerical data to an external (text) file.[/color]
      >
      > Use string formatting operations
      > http://docs.python.org/lib/typesseq-strings.html[/color]

      Great stuff. Thanks.

      Comment

      Working...