【发布时间】:2015-06-05 02:49:11
【问题描述】:
我正在玩 xlrd,但遇到了一些困难。我要做的主要思想是打开一个 Excel 文件,将第一列内容读入一个数组,然后使用该数组在另一个 Excel 文件中搜索。找到该值后,它应该从第二个 Excel 文件中返回三个单元格的内容。我的代码:
import xlrd
import os.path
from docx import Document
document = Document()
registry = xlrd.open_workbook('/root/Desktop/registry.xlsx')
findings = xlrd.open_workbook('/root/Desktop/findings.xlsx')
findings_sheet = findings.sheet_by_index(0)
registry_sheet = registry.sheet_by_index(0)
num_rows = findings.nrows - 1
curr_row = 0
findings_array = []
while curr_row < num_rows:
row = findings.row(curr_row)
findings_array += row
curr_row += 1
for finding in findings_array:
for r in range(first_sheet.nrows):
cell_col1=first_sheet.cell(rowx=r,colx=0).value
if cell_col1 == finding:
print first_sheet.cell(r,3)
print first_sheet.cell(r,4)
print first_sheet.cell(r,5)
else:
print "Finding not found"
目前它不起作用。如果我将 if cell_col1 == finding: 替换为 if cell_col1 == "ABC": 则条件有效,但它会打印出单元格 5 次,这是我的数组中的发现数。
我知道我的代码有问题,但我不是程序员,我有点卡住了。
【问题讨论】: