i playing around xlrd , having difficulties. main idea of trying open excel file, read first column contents array , use array search in excel file. when value found should return contents of 3 cells second excel file. code:
import xlrd import os.path 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 finding in findings_array: 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"
currently doesn't work. if replace if cell_col1 == finding:
if cell_col1 == "abc":
condition works prints out cells 5 times number of findings in array.
i know have issues in code not programmer , kinda stuck.
i can't run code , rewrite in more pythonic-style:
import xlrd import os.path registry_doc = xlrd.open_workbook('/tmp/s.xlsx') findings_doc = xlrd.open_workbook('/tmp/f.xlsx') findings_sheet = findings_doc.sheet_by_index(0) registry_sheet = registry_doc.sheet_by_index(0) findings = {findings_sheet.cell_value(i, 0) in range(0, findings_sheet.nrows)} r in range(0, registry_sheet.nrows): cell_col1=registry_sheet.cell_value(rowx=r,colx=0) if cell_col1 in findings: print registry_sheet.cell_value(r,2) print registry_sheet.cell_value(r,3) print registry_sheet.cell_value(r,4)
it must work
mistake in if cell_col1 == finding:
. comparing str
, list of cell
.
Comments
Post a Comment