[英]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. 我不想使用任何库。
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
...
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. 希望这对您有用。
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. 对结果满意后,可以删除DEBUG行。 They are there to show how the code works. 它们在那里显示代码如何工作。
For the heading, just select the text you want, like 对于标题,只需选择所需的文本,例如
select 'Name Gender Age ...';
To format the data, use the printf() function (see https://sqlite.org/lang_corefunc.html#printf ), like 要格式化数据,请使用printf()函数(请参阅https://sqlite.org/lang_corefunc.html#printf ),例如
select printf('%-20s %-6s %2d ....', Name, Gender, Age, ...) from ...;
(Adjust as necessary.) (根据需要进行调整。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.