【问题标题】:Convert rows of beautiful table to list将漂亮的表格行转换为列表
【发布时间】:2019-08-13 23:06:14
【问题描述】:

我想将beautifultable 的行转换为列表的元素,同时排除标题,例如:

 [['A',2,4], ['B',2,5], ['C',2,1]]

【问题讨论】:

    标签: python python-beautifultable


    【解决方案1】:

    打电话就行了

    list(map(list, table))

    完整代码:

    from beautifultable import BeautifulTable
    table = BeautifulTable()
    table.column_headers = ["c1", "c2", "c3"]
    table.append_row(['A', 2, 4])
    table.append_row(['B', 2, 5])
    table.append_row(['C', 2, 6])
    print(table)
    # it will print
    # +----+----+----+
    # | c1 | c2 | c3 |
    # +----+----+----+
    # | A  | 2  | 4  |
    # +----+----+----+
    # | B  | 2  | 5  |
    # +----+----+----+
    # | C  | 2  | 6  |
    # +----+----+----+
    li = list(map(list, table))
    print(li)
    # it will print
    # [['A', 2, 4], ['B', 2, 5], ['C', 2, 6]]
    

    【讨论】:

      【解决方案2】:

      如果您尝试获取行,Beautifultable 会给出以下结果:

          list([r for r in table])
      
       => [RowData<'A',2,4>, RowData<'B',2,5>, RowData<'C',2,1>]  
      

      将其转换为形式:[['A',2,4], ['B',2,5], ['C',2,1]]

      用途:

      list([list(r) for r in table])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多