Search code examples
pythonsqliteformat

printing table in format without using a library, SQLite 3 python


I want to print data from my database in a better format, this is for a game I made. This is the code:

def read_all_tables(self):
        self.cursor.execute('SELECT Name, Gender, Age, Score, Date, Time FROM Link JOIN Scores ON  Score_ID = Scores.ID JOIN Player ON Player_ID = Player.id ORDER BY Score DESC')
        Data = self.cursor.fetchall()
        for Row in Data:
            print()
            for record in range(len(Row)):
               print(Row[record], end=" ")

The output is:

HAMMAD MALE 18 900 07/01/18 13:07:02 
HAMMAD MALE 18 850 07/01/18 13:30:11 
INDERVEER MALE 18 750 07/01/18 13:35:46 
HAMMAD MALE 18 500 07/01/18 13:08:29 
HAMMAD MALE 18 400 07/01/18 14:07:29 
PARSA MALE 18 300 07/01/18 13:36:58 
HADIA FEMALE 19 300 07/01/18 14:09:37 
MANAAL FEMALE 18 100 07/01/18 13:51:40 
MICHAL MALE 18 0 07/01/18 13:42:41 
HAMMAD MALE 18 0 07/01/18 13:44:04 
HADIA FEMALE 19 0 07/01/18 13:45:51 
MANAAL FEMALE 18 0 07/01/18 13:53:02 
JACK WEIRD 21 0 07/01/18 13:53:49 
HAMMAD MALE 18 0 07/01/18 13:54:44 
HAMMAD MALE 18 0 07/01/18 13:56:08 
MANAAL FEMALE 18 0 07/01/18 13:57:39 
PARSA MALE 18 0 07/01/18 13:58:25 
HAMMAD MALE 18 0 07/01/18 13:59:08 
HAMMAD MALE 18 0 07/01/18 14:10:37 

How can I align them and have a column heading? I would like not to use any library.


Solution

  • Use the string formatting capability:

    formatted_row = '{:<10} {:<6} {:>6} {:>6} {:<9} {:<9}'
    print(formatted_row.format("Name", "Gender", "Age", "Score", "Date", "Time"))
    for Row in Data:
        print(formatted_row.format(*Row))
    

    Output:

    Name       Gender    Age  Score Date      Time     
    HAMMAD     MALE       18    900 07/01/18  13:07:02 
    HAMMAD     MALE       18    850 07/01/18  13:30:11 
    INDERVEER  MALE       18    750 07/01/18  13:35:46 
    HAMMAD     MALE       18    500 07/01/18  13:08:29 
    HAMMAD     MALE       18    400 07/01/18  14:07:29 
    PARSA      MALE       18    300 07/01/18  13:36:58 
    HADIA      FEMALE     19    300 07/01/18  14:09:37 
    MANAAL     FEMALE     18    100 07/01/18  13:51:40 
    ...
    

    Note

    In this approach, we hard-code the width of the columns. In order to dynamically adjust the columns width, we will have to do a good deal more works. I hope this will work for you.

    Update

    In order to dynamically adjust the widths, we need to pass through the data twice: The first time to determine the maximum width for each column, and the second to print.

    # Determine the longest width for each column
    header = ("Name", "Gender", "Age", "Score", "Date", "Time")
    widths = [len(cell) for cell in header]
    for row in Data:
        for i, cell in enumerate(row):
            widths[i] = max(len(str(cell)), widths[i])
    
    # Construct formatted row like before
    formatted_row = ' '.join('{:%d}' % width for width in widths)
    
    print('DEBUG: widths={!r}'.format(widths))
    print('DEBUG: formatted_row={!r}'.format(formatted_row))
    
    print(formatted_row.format(*header))
    for row in Data:
        print(formatted_row.format(*row))
    

    Output:

    DEBUG: widths=[9, 6, 3, 5, 8, 8]
    DEBUG: formatted_row='{:9} {:6} {:3} {:5} {:8} {:8}'
    Name      Gender Age Score Date     Time    
    HAMMAD    MALE    18   900 07/01/18 13:07:02
    HAMMAD    MALE    18   850 07/01/18 13:30:11
    INDERVEER MALE    18   750 07/01/18 13:35:46
    ...
    

    Once you are happy with the result, you can delete the DEBUG lines. They are there to show how the code works.