Python 漂亮的分开每个 Table
Python Prettytable Separating Each Table
我有一个小问题。如果 table 具有相同的列名,我该如何拆分?
这是示例:
+----------------------------+
| AUDIO TRACKS |
+----+----------+------------+
| ID | LangCode | LangName |
+----+----------+------------+
| 1 | de | German |
| 2 | de | German |
| 3 | en | English |
| 4 | en | English |
| 5 | es | Spanish |
| 6 | es | Spanish |
+----------------------------+
这是我想要的输出
+----------------------------+
| AUDIO TRACKS |
+----+----------+------------+
| ID | LangCode | LangName |
+----+----------+------------+
| 1 | de | German |
| 2 | de | German |
|----------------------------|
| 3 | en | English |
| 4 | en | English |
|----------------------------|
| 5 | es | Spanish |
| 6 | es | Spanish |
+----------------------------+
您可以使用 itertools.groupby
执行此操作,并在每个组之后添加 split_row,如下所示:
from prettytable import PrettyTable
from itertools import groupby
myTable = PrettyTable(["ID", "LongCode", "LangName"])
lst = [["1", "de", "German"], ["2", "de", "German"],
["3", "en", "English"], ["4", "en", "English"],
["5", "es", "Spanish"], ["4", "es", "Spanish"]]
split_row = ['—' * x for x in [2, 8, 10]]
myTable.title = 'AUDIO TRACKS'
for key, group in groupby(lst, lambda x: x[1]):
for g in group:
myTable.add_row(g)
myTable.add_row(split_row)
print(myTable)
输出:
+----------------------------+
| AUDIO TRACKS |
+----+----------+------------+
| ID | LongCode | LangName |
+----+----------+------------+
| 1 | de | German |
| 2 | de | German |
| —— | ———————— | —————————— |
| 3 | en | English |
| 4 | en | English |
| —— | ———————— | —————————— |
| 5 | es | Spanish |
| 4 | es | Spanish |
| —— | ———————— | —————————— |
+----+----------+------------+
我有一个小问题。如果 table 具有相同的列名,我该如何拆分?
这是示例:
+----------------------------+
| AUDIO TRACKS |
+----+----------+------------+
| ID | LangCode | LangName |
+----+----------+------------+
| 1 | de | German |
| 2 | de | German |
| 3 | en | English |
| 4 | en | English |
| 5 | es | Spanish |
| 6 | es | Spanish |
+----------------------------+
这是我想要的输出
+----------------------------+
| AUDIO TRACKS |
+----+----------+------------+
| ID | LangCode | LangName |
+----+----------+------------+
| 1 | de | German |
| 2 | de | German |
|----------------------------|
| 3 | en | English |
| 4 | en | English |
|----------------------------|
| 5 | es | Spanish |
| 6 | es | Spanish |
+----------------------------+
您可以使用 itertools.groupby
执行此操作,并在每个组之后添加 split_row,如下所示:
from prettytable import PrettyTable
from itertools import groupby
myTable = PrettyTable(["ID", "LongCode", "LangName"])
lst = [["1", "de", "German"], ["2", "de", "German"],
["3", "en", "English"], ["4", "en", "English"],
["5", "es", "Spanish"], ["4", "es", "Spanish"]]
split_row = ['—' * x for x in [2, 8, 10]]
myTable.title = 'AUDIO TRACKS'
for key, group in groupby(lst, lambda x: x[1]):
for g in group:
myTable.add_row(g)
myTable.add_row(split_row)
print(myTable)
输出:
+----------------------------+
| AUDIO TRACKS |
+----+----------+------------+
| ID | LongCode | LangName |
+----+----------+------------+
| 1 | de | German |
| 2 | de | German |
| —— | ———————— | —————————— |
| 3 | en | English |
| 4 | en | English |
| —— | ———————— | —————————— |
| 5 | es | Spanish |
| 4 | es | Spanish |
| —— | ———————— | —————————— |
+----+----------+------------+