如何在 Python 中找到 Excel 檔案的第一行空行?
在本文中,我們將向您展示如何使用 Python 查詢給定 Excel 檔案中第一行空行的索引。
假設我們有一個名為 demoTutorialsPoint.xlsx 的 Excel 檔案,其中包含一些隨機資料和一些空行。我們將返回 Excel 工作表中第一行空行的索引。
sampleTutorialsPoint.xlsx
| 球員姓名 | 年齡 | 型別 | 國家 | 隊伍 | 得分 | 擊球次數 |
|---|---|---|---|---|---|---|
| Virat Kohli | 33 | 擊球手 | 印度 | 皇家挑戰者班加羅爾 | 6300 | 20 |
| Bhuvaneshwar Kumar | 34 | 擊球手 | 印度 | 太陽昇起海德拉巴 | 333 | 140 |
| Mahendra Singh Dhoni | 39 | 擊球手 | 印度 | 欽奈超級國王 | 4500 | 0 |
| Rashid Khan | 28 | 投球手 | 阿富汗 | 古吉拉特巨人 | 500 | 130 |
| David Warner | 34 | 擊球手 | 澳大利亞 | 德里首都 | 5500 | 12 |
| Kieron Pollard | 35 | 全能型球員 | 西印度群島 | 孟買印第安人 | 3000 | 67 |
| Kagiso Rabada | 29 | 投球手 | 南非 | 勒克瑙首都 | 335 | 111 |
演算法(步驟)
以下是執行所需任務需要遵循的演算法/步驟:
透過將程式碼寫入 try-except 塊中來處理錯誤/異常。
使用 import 關鍵字匯入 xlrd 模組(要讀取電子表格中的資料,請使用 xlrd 模組。它具有讀取、寫入和更改資料的能力。此外,使用者可能需要遍歷多個工作表以根據某些條件獲取資料或更改特定行和列等。使用 xlrd 模組從電子表格中提取資料)。
pip install xlrd
建立一個具有隨機名稱的函式,例如 firstEmptyRow()。此函式返回在輸入 Excel 檔案中找到的第一行空行的索引。
建立一個變數來儲存 Excel 表格中存在的空單元格的數量。
建立一個變數來儲存輸入 Excel 檔案的路徑。
要建立工作簿物件,請將輸入 Excel 檔案傳遞給 xlrd 模組的 open_workbook() 函式(開啟工作簿)。
使用 sheet_by_index() 方法(開啟具有特定索引的工作表),開啟給定工作簿中的第一個工作表(這裡 0 代表第一個工作表)。
使用 for 迴圈遍歷工作表中的所有行。nrows 屬性用於獲取總行數。
使用巢狀的 for 迴圈,使用另一個巢狀的 for 迴圈遍歷工作表的所有列。ncols 屬性用於獲取總列數。
使用 cell_value() 函式(給出指定行和列中單元格的值)和 if 條件語句,確定單元格是否為空或不為空。
如果它是一個空單元格,則將空單元格計數增加 1。
檢查空單元格的數量是否等於 Excel 檔案中的列數(這表示該行是空行),如果為真,則返回當前行的索引。
呼叫 firstEmptyRow() 函式(它給出行號的索引)並建立一個變數來儲存它。
將行號加 1,因為它是從 0 開始的索引。
列印空行的結果。
如果沒有空行,則該函式不會返回任何內容,從而導致異常,我們在 except 塊中處理該異常。
示例
以下程式列印在輸入 Excel 檔案中找到的第一行空行的索引,如果 Excel 檔案中沒有空行,它會透過列印隨機文字 0 來處理錯誤。
try: # import xlrd module import xlrd # This function returns the index of the first empty row def firstEmptyRow(): # storing the count of empty cells emptycellscount = 0 # input excel file path inputExcelFile ="sampleTutorialsPoint.xlsx" # creating/opening a workbook new_workbook = xlrd.open_workbook(inputExcelFile) # Opening the first worksheet in the workbook firstWorksheet =new_workbook.sheet_by_index(0) # Traversing in all the rows of the worksheet # (nrows is used to get the number of rows) for each_row in range(firstWorksheet.nrows) : # Traversing in all the columns of the worksheet # (ncols is used to get the number of columns) for each_col in range (firstWorksheet.ncols) : # Checking whether the cell is a blank/empty cell if(firstWorksheet.cell_value(each_row, each_col)=="") : emptycellscount +=1 # Checking whether the number of empty cells is equal to the number of columns # (If they are equal, the row is blank) if (emptycellscount==firstWorksheet.ncols): return each_row # Calling the above firstEmptyRow() function to get the index of the first blank row row_number=firstEmptyRow() # Increment the row number by 1(as it is 0-based indexing) row_number=row_number+1 print("The first empty row is found in row number:", row_number) # If the function doesn't return anything then there is no blank row except: print("Hey User! there is No Empty Row in a given excel file")
輸出
執行上述程式後,將生成以下輸出:
The first empty row is found in row number: 6
在我們的程式中,我們使用了包含虛擬資料的示例 Excel 檔案。Excel 檔案包含空行。我們使用一個變數來計算空單元格的數量,並使用 for 迴圈逐個單元格遍歷 Excel 檔案,檢查它是否為空單元格,計算該單元格,並確定空單元格的數量是否等於列數(這是空行的條件),最後列印第一行空行的行號。
結論
我們學習瞭如何利用 xlrd 模組從 Excel 檔案建立工作簿,以及從選定的工作表建立工作表。我們還學習瞭如何逐個單元格遍歷 Excel 檔案,如何使用列數邏輯來確定某一行是否為空,以及如何在函式不返回任何內容時處理異常。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP