Here is a quick and simple Python class I hacked up to take comma separated values and reformat them to a fixed column text table. Supports multi-line rows, column width limits, and creates a header row automatically using the data from the first row of the CSV input.
I needed this for a little project so I coded it up. I haven’t done a lot of tests but it works just fine for formatting the associative arrays I have run through it.
The class supports multi-line rows, limiting the width of the column, and automatically creating a heading based on the keys from the associative array.
Text Table Formatted Output of above example:
+----------+----+---------------------+ | COMPANY | ID | BALANCE | +----------+----+---------------------+ | AIG | 1 | -$99,999,999,999.00 | | Wachovia | 2 | -$10,000,000.00 | | HP | 3 | $555,000.000.00 | | IBM | 4 | $12,000.00 | +----------+----+---------------------+
Full class source code after the jump…
(more…)