漂亮的 table 布局使标题在分界线内

Pretty table layout to make title inside the dividing lines

我有以下用于漂亮 table 的代码,它是这样的:

from prettytable import PrettyTable

myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
myTable.title = 'Big Bang Theory'
  
# Add rows
myTable.add_row(["Leanord", "X", "B", "91.2 %"])
myTable.add_row(["Penny", "X", "C", "63.5 %"])
myTable.add_row(["Howard", "X", "A", "90.23 %"])
myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
myTable.add_row(["Raj", "X", "B", "88.1 %"])
myTable.add_row(["Amy", "X", "B", "95.0 %"])
print(myTable)

这会产生以下结果 table:

+---------------------------------------------+
|               Big Bang Theory               |
+--------------+-------+---------+------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

我想知道是否可以进行以下布局:

+--------------(Big Bang Theory)--------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

请告知这样的布局是否可行

编辑 1

我添加了这个:

table_txt = myTable.get_string()

table_txt = table_txt.replace("+---------------------------------------------+\n|               Big Bang Theory               |\n+--------------+-------+---------+------------+", "+--------------(Big Bang Theory)--------------+")

print(table_txt)
+--------------(Big Bang Theory)--------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

这种方法假定 table 宽度是静态的,因此如果宽度发生变化,第一行的交界处不会与 table 的其余部分对齐。

  1. 将您的 table 导出为字符串,并像您已经做的那样将标题保留在变量中。
  2. 计算 table 的宽度。这可以通过在 \n 处拆分字符串并获取拆分数组中任何字符串的长度来轻松完成。
  3. 删除 table 的前 2 行。您可以使用 split 函数或做一些正则表达式。
  4. 编写一个函数来构建 - 的字符串,标题居中,宽度为 table。
  5. 打印字符串。

你可以这样做

#! /usr/bin/env python3
from prettytable import PrettyTable

myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
title = 'Big Bang Theory'
  
# Add rows
myTable.add_row(["Leanord", "X", "B", "91.2 %"])
myTable.add_row(["Penny", "X", "C", "63.5 %"])
myTable.add_row(["Howard", "X", "A", "90.23 %"])
myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
myTable.add_row(["Raj", "X", "B", "88.1 %"])
myTable.add_row(["Amy", "X", "B", "95.0 %"])
for n, l in enumerate(myTable.get_string().split('\n')):
    if n == 0:
        l = f"+{f'({title})'.center(len(l)-2, '-')}+"
    print(l)

它将 table 的行枚举为字符串,对于第一行,将标题居中并替换它。

+--------------(Big Bang Theory)--------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

适用于任何宽度。

编辑

不确定我是否理解你的评论,但我认为你的意思是将 table 作为字符串而不打印它,然后执行此操作

table_txt = ''
for n, l in enumerate(myTable.get_string().split('\n')):
    if n == 0:
        l = f"+{f'({title})'.center(len(l)-2, '-')}+"
    table_txt = f'{table_txt}{l}\n'

您可能会找到其他连接字符串的方法 here,但使用这些 table 应该不会有性能问题。