2013-02-18 59 views
1

我有特定結構的exel文件。 有第一行是標題第二個名稱和最後一個是值。 我需要只拿冠軍和值是不是如果Excel文件只有3行那麼難,但它可以是100行和columms,我需要得到的只是標題和價值觀,沒有任何名稱和空行Python/xrld讀取行和列

  1   2  3   
1 GroupOne  Empty  Empty 
2  Jonh  Elena  Mike  
3  45   100  500 
4  Empty  Empty  Empty 
5 GroupTwo  Empty  Empty 
6 Lisa   Ken  Lui 
7 100    300  400 

正如你所看到的兩個標題之間總是一個空行。 最後它會是這樣的:

 
GroupOne 
45 100 500 
GroupTwo 
100 300 400 

在此先感謝您。我會很感激一些幫助

回答

5

我覺得這website有一個例子,這將有助於你:

import xlrd 
workbook = xlrd.open_workbook('my_workbook.xls') 
worksheet = workbook.sheet_by_name('Sheet1') 
num_rows = worksheet.nrows - 1 
num_cells = worksheet.ncols - 1 
curr_row = -1 
while curr_row < num_rows: 
    curr_row += 1 
    row = worksheet.row(curr_row) 
    print 'Row:', curr_row 
    curr_cell = -1 
    while curr_cell < num_cells: 
    curr_cell += 1 
    # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank 
    cell_type = worksheet.cell_type(curr_row, curr_cell) 
    cell_value = worksheet.cell_value(curr_row, curr_cell) 
    print ' ', cell_type, ':', cell_value 

功能cell_type應該幫助你建立一個if語句,如if worksheet.cell_type != 0,從而跳過空白單元格。

+0

請更新鏈接。它現在顯示'404'錯誤。 – theausome 2018-02-10 10:10:25