MacRiogh
MacRiogh

Reputation: 15

searching excel using xlrd

I am trying to teach myself how to use xlrd for a (conceptually) simple task:

I want to take a string through raw_input from the user and search an excel sheet for the string.

when found I want the program to print the cell row only

here is my non-working code to start with:

import xlrd
from xlrd import open_workbook

book = open_workbook('simple.xls')

sheet = book.sheet_by_index(0)

city = raw_input("> ")
for rowi in range(sheet.nrows):
    row = sheet.row(rowi)
    for coli, cell in enumerate(row):
        if cell.value == city:
            loc = cell.row
            ??????????????
cell = sheet.cell(loc, 9)

print "The Ordinance Frequency is %r" % cell.value

Upvotes: 1

Views: 2333

Answers (1)

user3203010
user3203010

Reputation: 261

Try cycling through the columns in the same way that you cycle through rows

for r in range(sheet.nrows): for c in range(sheet.ncols): cell = sheet.cell(r, c) if cell.value == city: loc = r //index of interesting row

Upvotes: 1

Related Questions