将漂亮的 table 行转换为列表
Convert rows of beautiful table to list
我想将 beautifultable 的行转换为列表的元素,同时排除 header 例如:
[['A',2,4], ['B',2,5], ['C',2,1]]
如果您尝试获取行,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])
只要打电话
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]]
我想将 beautifultable 的行转换为列表的元素,同时排除 header 例如:
[['A',2,4], ['B',2,5], ['C',2,1]]
如果您尝试获取行,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])
只要打电话
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]]