Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python xlrd数据提取_Python_Xlrd - Fatal编程技术网

Python xlrd数据提取

Python xlrd数据提取,python,xlrd,Python,Xlrd,我正在使用python xlrd从excel工作表中读取数据 我的问题是,如果我在excel工作表中读取第一个单元格作为员工姓名的行 还有一行名为,其第一个单元格是Employee name 如何读取从第一个单元格中包含员工姓名的最后一行开始的最后一列。忽略前一行 wb = xlrd.open_workbook(file,encoding_override="cp1252") wb.sheet_names() sh = wb.sheet_by_index(0) num_of_

我正在使用python xlrd从excel工作表中读取数据

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

还有一行名为,其第一个单元格是Employee name

如何读取从第一个单元格中包含员工姓名的最后一行开始的最后一列。忽略前一行

  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"

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

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

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
我正在使用python xlrd从excel工作表中读取数据

你需要考虑你在做什么,而不是抓取一些博客代码,留下一些完全不相关的东西,比如wb.sheet\u名称,省略与你的需求非常相关的部分,比如first\u column=sh.col\u values0

以下是如何查找A列中最后一列(第一列为未测试列)的行索引:

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
现在,我们需要解释如何读取从第一个单元格中包含员工姓名的最后一行开始的最后一列

假设最后一列表示列_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))

避免说第一行第一个单元格是Emp名称,第二行第一个单元格是Emp名称,第三行第一个单元格是Emp名称。在这种情况下,我想从第三个单元格读取