打印表格式,不使用库,SQLite 3 python

时间:2018-01-07 14:17:47

标签: python sqlite format

我想以更好的格式从我的数据库打印数据,这是我制作的游戏。这是代码:

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=" ")

输出结果为:

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 

如何对齐它们并有一个列标题?我不想使用任何库。

2 个答案:

答案 0 :(得分:2)

使用string formatting功能:

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

输出:

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

注意

在这种方法中,我们硬编码列的宽度。为了动态调整列宽,我们将不得不做更多的工作。我希望这对你有用。

更新

为了动态调整宽度,我们需要两次传递数据:第一次确定每列的最大宽度,第二次打印。

# 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))

输出:

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

对结果感到满意后,您可以删除 DEBUG 行。它们用于显示代码的工作原理。

答案 1 :(得分:0)

对于标题,只需选择所需的文字,例如

select printf('%-20s %-6s  %2d ....', Name, Gender, Age, ...) from ...;

要格式化数据,请使用printf()函数(请参阅this),如

{{1}}

(必要时调整。)