2017-03-16 29 views
2

我正在使用xlrd通過目錄結構嗅探並提取電子表格,將第二行(第1行)讀到「執行任務」。問題是我不知道如何在第一個空行停止閱讀/打印。我知道行不是「空」對象,但我希望有一點幫助,說明如何檢查所有單元格是否爲空。這裏是我正在使用的代碼:如何在第一個空行停止使用xlrd讀取電子表格?

import xlrd 
import os 

def excel_file_filter(filename, extensions=['.xls', '.xlsx']): 
    return any(filename.endswith(e) for e in extensions) 

def get_filenames(root): 
    filename_list = [] 
    for path, subdirs, files in os.walk(root): 
     for filename in filter(excel_file_filter, files): 
      filename_list.append(os.path.join(path, filename)) 
    return filename_list 

spreadsheets = get_filenames('C:\\Temp') 
for s in spreadsheets: 
    with xlrd.open_workbook(s) as wb: 
     cs = wb.sheet_by_index(0) 
     num_cols = cs.ncols 
     for row_index in range(1, cs.nrows): 
      print('Row: {}'.format(row_index)) 
      for col_index in range(0, num_cols): 
       cell_object = cs.cell(row_index, col_index) 
       if cell_obj is not xlrd.empty_cell: 
        print('Col #: {} | Value: {}'.format(col_index, cell_obj)) 

什麼最終發生的是,它打印一路走過近1000行,當只有先說,25排在他們的內容。不同的電子表格之間的內容數量是不同的,所以一個通用的解決方案(不依賴於其他可選庫)可以幫助我理解如何檢測空行然後中斷。

+0

檢查這些問題,他們可能會有所幫助:[如何檢測一個單元格是否爲空...](http://stackoverflow.com/questions/11603009/how-to-detect-if-a-cell-is-empty-when-reading-excel-files -using-the-xlrd-library)和[使用XLRD驗證單元格值](http://stackoverflow.com/questions/29907072/validating-a-cell-value-using-xlrd)。如果這些都不能解決您的問題,我很樂意提供幫助。 – 2017-03-18 16:57:47

+0

@Mimx對不起,我應該提到我先檢查了那些。我確實理解了一些一般理論,但是所有嵌套循環都可以歸結爲單元格值,我很快就對xlrd感到非常不滿。我將不勝感激這種情況下的一些示範性幫助:) – auslander

回答

4

第一:要獲得那麼單元格的值是否是空的,使用的方法之一,有問題How to detect if a cell is empty when reading Excel files using the xlrd library?

  1. 的答案解釋當使用cell_val= cs.cell(row_index, col_index).value來獲取值:
    • 要檢查它是否爲空:只需寫入if cell_vel == ''
  2. 當使用cell_object = cs.cell(row_index, col_index)得到的值:
    • 要檢查是否爲空:
      - 首先獲得單元類型cell_type = cs.cell_type(row_index, col_index)
      - 然後檢查if cell_type == xlrd.XL_CELL_EMPTY

二:要檢查整排是空的,你可以做到以下幾點:

  1. 定義一個co溫特(count_empty = 0)來計算在行&布爾(empty_cell = FALSE)
  2. 入住的空細胞,如果細胞是空的
    如果是>增量計數器&變化empty_cell爲True
    如果沒有> empty_cell設置假
  3. 檢查empty_cell爲False>打印單元
  4. 的值通過列行
    如果count_empty等於列數的循環後>意味着整個行空>的突破,並停止通過循環行

驗證碼:

# define empty_cell boolean 
empty_cell= False 
with xlrd.open_workbook(s) as wb: 
    cs= wb.sheet_by_index(0) 
    num_cols= cs.ncols 
    num_rows= cs.nrows 
    for row_index in range(1, num_rows): 
     # set count empty cells 
     count_empty = 0 
     print('Row: {}'.format(row_index)) 
     for col_index in range(0,num_cols): 
      # get cell value 
      cell_val= cs.cell(row_index, col_index).value 
      # check if cell is empty 
      if cell_val== '': 
       # set empty cell is True 
       empty_cell = True 
       # increment counter 
       count_empty+= 1 
      else: 
       # set empty cell is false 
       empty_cell= False 

      # check if cell is not empty 
      if not empty_cell: 
       # print value of cell 
       print('Col #: {} | Value: {}'.format(col_index, cell_val)) 

     # check the counter if is = num_cols means the whole row is empty  
     if count_empty == num_cols: 
      print ('Row is empty') 
      # stop looping to next rows 
      break  

注:我用第一種方法cell_val= cs.cell(row_index, col_index).value獲得細胞的價值,我看到它的簡單。 如果你想使用另一種方法改變如下:幫助我瞭解如何檢查單元格是空的

cell_val= cs.cell(row_index, col_index) # remove .value 
    cell_type= cs.cell_type(row_index, col_index) # add this line 
    # check if cell is empty 
    if cell_type == xlrd.XL_CELL_EMPTY: # change if cell_val== '': 

其他鏈接:
xlrd.XL_CELL_EMPTYValidating a cell value using XLRD

+0

這是我需要打破logjam,並與代碼的其餘部分。不僅感謝代碼,而且感謝它解釋爲什麼它有效。在我看來,這種答案是stackoverflow的理想。感謝你的時間! – auslander

相關問題