Prettytable vrules and hrules to none

Posted

技术标签:

【中文标题】Prettytable vrules and hrules to none【英文标题】: 【发布时间】:2021-12-13 03:44:40 【问题描述】:

我有以下代码可以生成漂亮的表格。我想做的是删除 vrules 或 hrules。我拥有的代码没有做任何事情。你能告诉我为什么吗?只是为了确保,vrules 是垂直线

from prettytable import PrettyTable, FRAME, HEADER, NONE
PrettyTable().hrules = NONE
# x = PrettyTable()
# x.hrules = NONE
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 %"])
PrettyTable().vrules = NONE
print(myTable.get_string(sort_key=operator.itemgetter(1, 3), sortby="Section"))

它仍在生成同一张表:

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

【问题讨论】:

【参考方案1】:

根据docs你可以:

>>> myTable = PrettyTable()
>>> myTable.hrules, myTable.vrules = NONE, NONE
>>> myTable.title = 'Big Bang Theory' # this won't show, see below
>>> myTable.field_names = ["Student Name", "Class", "Section", "Percentage"]
>>> 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.get_string(sort_key=operator.itemgetter(1, 3), sortby="Section"))
  Student Name   Class   Section   Percentage  
      Amy          X        B        95.0 %    
   Bernadette      X        D        92.7 %    
     Howard        X        A       90.23 %    
    Leanord        X        B        91.2 %    
     Penny         X        C        63.5 %    
      Raj          X        B        88.1 %    
    Sheldon        X        A        98.2 %  

上面有问题。也就是说,它将显示标题。要显示标题,您需要安装PTable。一旦你执行了上面的代码,就会产生:

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

【讨论】:

以上是关于Prettytable vrules and hrules to none的主要内容,如果未能解决你的问题,请参考以下文章

python---PrettyTable

Windows下 prettytable模块安装

python之PrettyTable模块

python模块——PrettyTable

Python prettytable模块

Python模块之"prettytable"