如何 select 所有具有相同名称的行而不仅仅是第一行?

How to select all the rows with the same name and not only the first one?

这是 xls 文件:

moto    5   2   45
moto    2   4   43
coche   8   54  12
coche   43  21  6
coche   22  14  18

这是使用 pyexcel 库的代码:

import pyexcel as pe
data = pe.get_sheet(file_name="vehiculo.xls")
sheet = pe.Sheet(data, name_rows_by_column=0)
sheet.row.select(['coche'])
sheet.save_as("output.xls")

它 returns 只有名称为 'coche' 的第一行:

coche   8   54  12

我想要名称为 "coche" 的所有行。

有什么想法吗? 谢谢

只需遍历数据:

import pyexcel
from collections import defaultdict

results = defaultdict(list)
data = pe.get_sheet(file_name="vehiculo.xls")
for row in data.rows():
   results[row[0]] += row[1:]

您将获得以下内容:

>>> results['moto']
[5L, 2L, 45L, 2L, 4L, 43L]

对于函数 'select' 没有按预期工作这一事实,我深表歉意。它不起作用,因为该行的其余部分变为 coche-1,coche-2(您可以通过 print(s) 查看)。这是具有相同名称的行名称的默认行为。将来会改变。

现在,要让您的用例正常工作,您可以使用 pyexcel==0.3.0+ 和以下语句:

...
>>> sheet = pe.Sheet(data) # , name_rows_by_column=0)
>>> del sheet.row[lambda row_inde,row: row[0] != 'coche']
...

the documentation 介绍此功能。