FORTRAN like formatting

Cyril Bazin cyril.bazin at gmail.com
Sat Jul 9 10:41:21 EDT 2005


Ok, dennis, your solution may be better, but is quite dangerous:
Python can't handle if there is exactly 3 arguments passed to the
function. The created code is correct but the error will appear when
your run Fortran.

Cyril

On 7/9/05, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
> On Fri, 8 Jul 2005 20:31:06 +0200, Cyril BAZIN <cyril.bazin at gmail.com>
> declaimed the following in comp.lang.python:
> 
> 
> >
> > def toTable(n1, n2, n3):
> >     return "%20s%20s%20s"%tuple(["%.12f"%x for x in [n1, n2, n3]])
> >
>         Ugh...
> 
> def toTable(*ns):
>         return ("%20.12f" * len(ns)) % ns
> 
> toTable(3.14, 10.0, 3, 4, 5.999999)
> '      3.140000000000     10.000000000000      3.000000000000'
> toTable(3.14, 10.0, 3, 4, 5.999999)
> '      3.140000000000     10.000000000000      3.000000000000
> 4.000000000000      5.999999000000'
> toTable(1)
> '      1.000000000000'
> 
>         The second one IS one line, it just wraps in the news message.
> Using the *ns argument definition lets the language collect all
> arguments into a tuple, using * len(ns) creates enough copies of a
> single item format to handle all the arguments.
> 
>         Oh, a top-poster... No wonder I didn't recall seeing any
> Fortran.
> 
> > On 7/8/05, Einstein, Daniel R <daniel.einstein at pnl.gov> wrote:
> > >
> > >
> > > Hi,
> > >
> > > Sorry for this, but I need to write ASCII from my Python to be read by
> > > FORTRAN and the formatting is very important. Is there any way of doing
> > > anything like:
> > >
> > > write(*,'(3(" ",1pe20.12))') (variable)
> > >
> 
>         Which Fortran compiler? I know VMS Fortran was very friendly,
> when specifying "blanks not significant" or something like that... To
> read three floating numbers (regardless of format) merely required
> something like:
> 
>         read(*, '(bn,3f)') a, b, c
> 
> (or 'bs' for blanks significant -- I forget which one enabled free
> format input processing)
> 
>         You aren't going to get prescaling; Python follows C format
> codes (if one doesn't use the generic %s string code and rely on Python
> to convert numerics correctly).
> 
> def toTable(*ns):
>         return ("%20.12e" * len(ns)) % ns
> 
> toTable(3.14, 10.0, 3)
> ' 3.140000000000e+000 1.000000000000e+001 3.000000000000e+000'
> 
> --
>  > ============================================================== <
>  >   wlfraed at ix.netcom.com  | Wulfraed  Dennis Lee Bieber  KD6MOG <
>  >      wulfraed at dm.net     |       Bestiaria Support Staff       <
>  > ============================================================== <
>  >           Home Page: <http://www.dm.net/~wulfraed/>            <
>  >        Overflow Page: <http://wlfraed.home.netcom.com/>        <
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list