0

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

2 Answers 2

2

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))
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately that way assumes a prior knowledge of how many columns need to be formatted. IN this case I have 4 columns, but the function can allow for any number of columns to be fed into the function and the code has to be smart enough to know how to format the output for each circumstance.
This solution is not any better than the one I used first. If I narrow the length to 15, the Column1 still takes up more than 15 columns, which it should not do, when I specify new_row={:<15} * (len(headers)).
Disregard my prior comment, I see you made change to your response. I think this works very well now. Thank you for the guidance.
Actually I take my previous comment back. This solution still causes the 3rd column to exceed 15 columns. If I go to 20 columns there is space, but nonetheless, I need the column to stop at 15 and still have a space between each column.
@Jon yeah, never mind. although I haven't read through your question in detail, I just wrote some codes to show you can use String format in these kind of ways. :)
1

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   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.