Search code examples
pythonprettytable

Prettytable vrules and hrules to none


I have the following code which produces pretty table. What i would like to do is to remove the vrules or hrules. The code i have is not doing anything. Could you please advise why? Just to make sure, vrules is the vertical lines

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

It is still producing the same table:

+---------------------------------------------+
|               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 %   |
+--------------+-------+---------+------------+

Solution

  • According to the docs you can:

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

    The above has an issue. That is, namely, that it will not show the title. To have the title shown you need to install PTable. Once you do that the code above, produces:

                    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 %