代码之家  ›  专栏  ›  技术社区  ›  Slartibartfast

可预处理的vrules和hfules到none

  •  1
  • Slartibartfast  · 技术社区  · 3 年前

    我有下面的代码,它产生了漂亮的表。我想做的是去除这些褶皱或灌木。我的代码什么都没做。你能告诉我为什么吗?只是为了确保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 %   |
    +--------------+-------+---------+------------+
    
    0 回复  |  直到 3 年前
        1
  •  2
  •   Nikolaos Chatzis    3 年前

    根据 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 %    
    
    推荐文章