Re: FORTRAN like formatting
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 [EMAIL PROTECTED] wrote: On Fri, 8 Jul 2005 20:31:06 +0200, Cyril BAZIN [EMAIL PROTECTED] 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.99) ' 3.1400 10. 3.' toTable(3.14, 10.0, 3, 4, 5.99) ' 3.1400 10. 3. 4. 5.9900' toTable(1) ' 1.' 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 [EMAIL PROTECTED] 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.1400e+000 1.e+001 3.e+000' -- == [EMAIL PROTECTED] | Wulfraed Dennis Lee Bieber KD6MOG [EMAIL PROTECTED] | 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: FORTRAN like formatting
Dennis Lee Bieber wrote: On 7/8/05, Einstein, Daniel R [EMAIL PROTECTED] 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) Fortran 77 and later versions have list-directed I/O, so the OP could simply write read (inunit,*) a,b,c if the numbers in his input file are separated by spaces or commas. An online reference is http://docs.hp.com/cgi-bin/doc3k/B3150190022.12120/9 . -- http://mail.python.org/mailman/listinfo/python-list
FORTRAN like formatting
Title: FORTRAN like formatting 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) In other words, I want three columns 20 spaces long, with 12 digits after the decimal and so on and so forth. What I am really looking for is some general indication of how to do such formatting in Python. Any help? Dan Daniel R Einstein, PhD Biological Monitoring and Modeling Pacific Northwest National Laboratory P.O. Box 999; MSIN P7-59 Richland, WA 99352 Tel: 509/ 376-2924 Fax: 509/376-9064 [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: FORTRAN like formatting
Hello, I don't anderstand very well Fortran syntax, but want you say something like that: def toTable(n1, n2, n3): return %20s%20s%20s%tuple([%.12f%x for x in [n1, n2, n3]]) Example: import math toTable(math.pi, 10, 8.2323) ' 3.141592653590 10. 8.2323' If it is not that, please could you give an example of input and output of your code? Cyril On 7/8/05, Einstein, Daniel R [EMAIL PROTECTED] 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) In other words, I want three columns 20 spaces long, with 12 digits after the decimal and so on and so forth. What I am really looking for is some general indication of how to do such formatting in Python. Any help? Dan Daniel R Einstein, PhD Biological Monitoring and Modeling Pacific Northwest National Laboratory P.O. Box 999; MSIN P7-59 Richland, WA 99352 Tel: 509/ 376-2924 Fax: 509/376-9064 [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list