如何在 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 文件,如何使用列数逻辑来确定某一行是否为空,以及如何在函数不返回任何内容时处理异常。

更新于: 2022年8月18日

3K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告
© . All rights reserved.