Rather than create for-loops/nested for-loops, is there a more Pythonic way to write a fixed-width string using a meta-data dictionary?
My input is as follows:
{
't_order': 11112014,
't_date': 20150101,
't_external': 'from sample',
't_mode': 'A',
'message_id': 'ID01',
't_value': 123.45
}
And my meta-dict looks as such:
[{'field': 'message_id',
'decimalPrecision': '0',
'isTypeOf': 'C',
'Length': '8',
'Level': '0',
'Start': '1'},
{'field': 't_mode',
'decimalPrecision': '0',
'isTypeOf': 'C',
'Length': '1',
'Level': '0',
'Start': '9'},
{'field': 't_order',
'decimalPrecision': '0',
'isTypeOf': '9',
'Length': '8',
'Level': '0',
'Start': '10'},
{'field': 't_external',
'decimalPrecision': '0',
'isTypeOf': 'C',
'Length': '25',
'Level': '0',
'Start': '18'},
{'field': 't_date',
'decimalPrecision': '0',
'isTypeOf': '9',
'Length': '8',
'Level': '0',
'Start': '43'},
{'field': 't_value',
'decimalPrecision': '4',
'isTypeOf': '9',
'Length': '18',
'Level': '0',
'Start': '51'}]
Anything of isTypeOf == C
is a str
and isTypeOf == 9
is an int
.
The Start
value is the start of the string position and Length
is the length of the field that is left-space-padded. The numeric field doesn't include a decimal point and is right-zero-padded in the precision. That being said, the given sample would read:
ID01 A11112014from sample 20150101 1234500
What would be a more efficient way instead of loops/nested for-statements?