Module Scientific.IO.FortranFormat

Fortran-compatible input/output

This module provides two classes that aid in reading and writing Fortran-formatted text files.

Examples:

Input:

s = '   59999'
format = FortranFormat('2I4')
line = FortranLine(s, format)
print line[0]
print line[1]

prints

5
9999

Output:

format = FortranFormat('2D15.5')
line = FortranLine([3.1415926, 2.71828], format)
print str(line)

prints

3.14159D+00 2.71828D+00


Class FortranLine: Fortran-style record in formatted files

FortranLine objects represent the content of one record of a Fortran-style formatted file. Indexing yields the contents as Python objects, whereas transformation to a string (using the built-in function str) yields the text representation.

Constructor: FortranLine(data, format, length=80)

data

either a sequence of Python objects, or a string formatted according to Fortran rules

format

either a Fortran-style format string, or a FortranFormat object. A FortranFormat should be used when the same format string is used repeatedly, because then the rather slow parsing of the string is performed only once.

length

the length of the Fortran record. This is relevant only when data is a string; this string is then extended by spaces to have the indicated length. The default value of 80 is almost always correct.

Restrictions:

1) Only A, D, E, F, G, I, and X formats are supported (plus string constants for output).

2) No direct support for complex numbers; they must be split into real and imaginary parts before output.

3) No overflow check. If an output field gets too large, it will take more space, instead of being replaced by stars according to Fortran conventions.


Class FortranFormat: Parsed fortran-style format string

Constructor: FortranFormat(format), where format is a format specification according to Fortran rules.