Jon
Jon

Reputation: 1801

Formatting repeated strings with python

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...

Upvotes: 0

Views: 118

Answers (2)

r.ook
r.ook

Reputation: 13858

Your code threw me for a loop with some typos and not matching the expected results, but I think I figured it out.

@Gordon's solution should work for you, but if you want to keep it in the spirit of your original code, the fmat line should be changed to this:

fmat = '{:15.15} ' * len(headers)

A quick google search led me here on how to combine truncating and padding with .format. In short, the :15 denotes the padding to 15 space at least, and the .15 denotes truncate to 15 chars at most.

You might also want to update your dividers line to this instead:

dividers = ['-' * len(i) for i in headers]

The code you showed only returned --- 4 times instead of - that matches the column's length.

The result is thus:

id              Date            Column1         Column2         
--              ----            -------         -------         
1               2017-01-18      7.7132064326674 Gas             
2               2017-01-13      0.2075194935940 Groceries       
3               2017-01-18      6.3364823492627 Groceries   

Upvotes: 1

Gordon
Gordon

Reputation: 576

You can use format in this way:

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']]

row_format ="{:<20}" * (len(headers))
print (row_format.format(*headers))
print (row_format.format(*dividers))
for row in data:
    print (row_format.format(*row))

Upvotes: 2

Related Questions