2

我正在使用 python xlrd http://scienceoss.com/read-excel-files-from-python/从 excel 表中读取数据

我的问题是,如果我在 Excel 表中读取第一个单元格为“员工姓名”的行

还有另一行命名,其第一个单元格是“员工姓名”

我们如何从第一个单元格中具有“员工姓名”的最后一行开始读取最后一列。忽略上一个

  wb = xlrd.open_workbook(file,encoding_override="cp1252") 
  wb.sheet_names()
  sh =  wb.sheet_by_index(0)
  num_of_rows = sh.nrows
  num_of_cols = sh.ncols
  valid_xl_format = 0
  invalid_xl_format = 0

  if(num_of_rows != 0):
     for i in range(num_of_rows):
        questions_dict = {}
        for j in range(num_of_cols):
              xl_data=sh.cell(i,j).value
              if ((xl_data == "Employee name")):
                  # Regardless of how many "Employee name" found in rows first cell,Read only the last "Employee name"
4

2 回答 2

5

我正在使用 python xlrd http://scienceoss.com/read-excel-files-from-python/从 excel 表中读取数据

您需要考虑自己在做什么,而不是获取一些博客代码并留下完全不相关的内容,例如wb.sheet_names()省略与您的需求非常相关的部分,例如first_column = sh.col_values(0).

以下是如何在 A 列(第一列)中找到最后一个“whatever”的 row_index - 未经测试:

import xlrd
wb = xlrd.open_workbook(file_name)
# Why do you think that you need to use encoding_overide?
sheet0 = wb.sheet_by_index(0)
tag = u"Employee name" # or u"Emp name" or ...
column_0_values = sheet0.col_values(colx=0)
try:
    max_tag_row_index = column_0_values.rindex(tag)
    print "last tag %r found at row_index %d" % (
        tag, max_tag_row_index)
except IndexError:
    print "tag %r not found" % tag

现在我们需要解释“我们如何从第一个单元格中具有“员工姓名”的最后一行开始读取最后一列

假设“最后一列”是指 column_index == sheet0.ncols - 1 的那一列,则:

last_colx = sheet0.ncols - 1
required_values = sheet0.col_values(colx=last_colx, start_rowx=max_tag_row_index)
required_cells = sheet0.col_slice(colx=last_colx, start_rowx=max_tag_row_index)
# choose one of the above 2 lines, depending on what you need to do

如果这不是您的意思(这很可能,因为它忽略了一大堆数据(为什么您只想阅读最后一列?),请尝试用示例来解释您的意思。

可能您想遍历剩余的单元格:

for rowx in xrange(max_tag_row_index, sheet0.nrows): # or max_tag_row_index + 1
    for colx in xrange(0, sheet0.ncols):
        do_something_with_cell_object(sheet0.cell(rowx, colx))
于 2010-09-23T10:16:45.967 回答
0

很难准确理解你在问什么。
发布示例数据可能有助于使您的意图更加清晰。

您是否尝试过反向迭代数据集?例如:

for i in reversed(range(num_of_rows)):
    ...
    if xl_data == "Employee name":
        # do something 
        # then break since you've found the final "Employee Name"
        break
于 2010-09-23T05:54:46.070 回答