2016-10-04 45 views
0

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

它只返回最前一頁列名爲 '科切':

coche 8 54 12 

我想要所有名稱爲「coche」的行。

有什麼想法? 感謝

回答

0

只是遍歷數據:

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] 
0

我的事實,功能「選擇」沒有奏效道歉意。它不起作用,因爲該行的其餘部分變成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這個功能。

相關問題