I have a code that yields headers, divider and data as lists, or in the case of data as a list of lists. In the example I am showing, each list has 4 strings within them; however, this can vary from run to run. I want to write the data in columnar format to the command prompt but am having problems. A sample of my code is shown below.
headers = ['id', 'Date', 'Column1', 'Column2']
dividers = ['-' * len(headers) for i in range(len(headers))]
data = [['1', '2017-01-18', '7.71320643266746', 'Gas'],
['2', '2017-01-13', '0.207519493594015', 'Groceries',
['3', '2017-01-18', '6.336482349262754', 'Groceries']
I am trying to print the data with the following format;
fmat = '{15s}' * len(headers)
print(fmat.format(*headers))
print(fmat.format(*dividers))
for i in range(len(data)):
print(fmat.format(*data[i))
While not in the coding I also want one space padding between each of the columns, which each should be no more than 15 columns. In other words I want the output to look like this;
id Date Column2 Column3
-- ---- ------- -------
1 2017-01-18 7.7132064326674 Gas
2 2017-01-13 0.2075194935940 Groceries
3 2017-01-18 6.3364823492627 Groceries
Unfortunately the output looks like this;
id Date Column2 Column3
-- ---- ------- -------
1 2017-01-18 7.71320643266746Gas
2 2017-01-13 0.207519493594015Groceries
3 2017-01-18 6.336482349262754Groceries
As you can see the Column2 is not ending at 15 characters and is overflowing into Column3. How can I format this issue so that the columns stop at 15 and in a way that also allows for a 1 character padding between each column? I am trying to keep the solution generic for cases where there are 8 columns or 3 columns, etc...