【问题标题】:Is there a way to search for a value in an Excel spreadsheet and print out what row the value is in using Python?有没有办法在 Excel 电子表格中搜索一个值并使用 Python 打印出该值在哪一行?
【发布时间】:2019-07-30 16:16:28
【问题描述】:

我正在尝试使用 Python 在 Excel 电子表格中查找一个值,并让 Python 打印出该值所在的行。

例如,我知道数字 10 在我的电子表格的某一列中,但是有太多行无法手动搜索,所以我想使用 Python 找出它在哪一行。

【问题讨论】:

    标签: python excel xlrd


    【解决方案1】:

    因此,例如,如果您要在各种数字的电子表格中查找“10”,解决此问题的简单方法是使用 xlrd 库并遍历每个单元格并检查它是否包含“10”

    for row in range(sheet.nrows):
       for col in range(sheet.ncols):
          if sheet.cell_value(row, col) == 10:
             print row, col
    

    请记住,这不是最有效的方法。如果您有大量数据,您可以使用 pandas,将一个 csv 作为“Objective_Table”数据框导入,另一个作为“Search_Table”导入,然后使用合并功能。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      相关资源
      最近更新 更多