使用xlrd搜索excel

时间:2015-04-21 22:01:40

标签: python-2.7 xlrd

我正在努力教自己如何使用xlrd进行(概念上)简单的任务:

我想从用户处获取raw_input的字符串,并在excel表中搜索字符串。

发现时我希望程序只打印单元格行

这是我的非工作代码:

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

1 个答案:

答案 0 :(得分:1)

尝试以循环遍历行的方式循环遍历列

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