Is it possible to somehow "type" in python in fortran like this?
1 4.5656 2 24.0900 3 698.2300 4 -3.5000
So, decimal points are always in the same column, and we always get 3 or n decimal numbers?
thank
>>> '%11.4f' % -3.5 ' -3.5000'
or formatting a new style:
>>> '{:11.4f}'.format(-3.5) ' -3.5000'
more about format specifiers in documents .
You can also see the library fortranformaton the PyPI page or, if you want to completely recreate the text IO FORTRAN.
fortranformat
If you have questions, send me a letter (I wrote it).
for i in [(3, 4.534), (3, 15.234325), (10,341.11)]: ... print "%5i %8.4f" % i ... 3 4.5340 3 15.2343 10 341.1100
print "%10.3f" % f
f ( : %-10.3f ). 10 ( s > 10 ) 3 . :
f
%-10.3f
f = 698.230 # <-- 7 characters when printed with %10.3f print "%10.3f" % f # <-- will print " 698.2300" (two spaces)
:
print "\n".join(map(lambda f: "%10.3f" % f, [4.5656, 24.09, 698.23, -3.5]))
string.rjust(), :
a = 4.5656 b = 24.0900 c = 698.2300 d = -3.5000 a = "%.4f" % a b = "%.4f" % b c = "%.4f" % c d = "%.4f" % d l = max(len(a), len(b), len(c), len(d)) for i in [a, b, c, d]: print i.rjust(l+2)
~ $ python test.py 4.5656 24.0900 698.2300 -3.5000
Fortran io C io.
fortranformat Brendan.
https://pypi.python.org/pypi/fortranformat
easy_install fortranformat
fortran , C.