Python 如何找到Excel文件的第一个空行

Python 如何找到Excel文件的第一个空行

在本文中,我们将展示如何使用Python找到给定Excel文件中第一个空行的索引。

假设我们已经有一个名为 demoTutorialsPoint.xlsx 的Excel文件,其中包含一些随机数据和一些空行。我们将返回Excel工作表中第一个空行的索引。

sampleTutorialsPoint.xlsx

球员姓名 年龄 类型 国家 球队 得分 wickets
维拉特·科尔希 33 板球手 印度 皇家查勘者班加罗尔 6300 20
布鲁万尼什瓦尔·库马尔 34 板球手 印度 太阳升之子海得拉巴 333 140
马亨德拉·辛格·多尼 39 板球手 印度 钦奈超王者 4500 0
拉希德·汗 28 球员 阿富汗 古吉拉特泰坦 500 130
大卫·华纳 34 板球手 澳大利亚 德里首都 5500 12
基伦·波拉德 35 全能手 西印度群岛 孟买印度人 3000 67
卡吉索·拉巴达 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文件的单元格,如何使用列数逻辑来判断一行是否为空,以及如何处理函数返回值为空的异常情况。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程