A class to write fixed-width files with an interface similar to python`s csv writer
The constructor takes the following arguments:
The file descriptor to write to.
A list of tuples containing the title of the column and the width of the corresponding field. For example:
cols = [
('ID', 5),
('NAME', 40),
]
Can optionally contain a options
dictionary containing the keys direction
and decimal_spaces
.
The direction
key accepts the character '>'
to right-align or the character '<'
to left-align.
Defaults to '<'
when omitted.
Note that when using the decimal_spaces
key, the length of the integer part plus the length of the fractional part plus one, for the decimal mark, must not exceed the set column width.
If omitted, defaults to unicode(value)
behaviour.
For example:
cols = [
('BALANCE', 6, {'direction': '>', 'decimal_spaces': 2}),
]
Will result in a right-aligned, 6-wide column. The value will be "quantized" to two decimal spaces.
(Optional) The line terminator to append to each line. Can be any string.
Defaults to '\r\n'
A dictonary with keys corresponding to the defined column names and their respective values. Extra keys are ignored.
A list of dictionaries with the format accepted by writerow
.
Check out the tests and the file example.py
for examples on how to use the features.