【问题标题】:searching excel using xlrd使用 xlrd 搜索 excel
【发布时间】:2015-04-24 03:17:10
【问题描述】:

我正在尝试自学如何使用 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

【问题讨论】:

    标签: python-2.7 xlrd


    【解决方案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

    【讨论】:

    • 你能解释一下下面一行:cell = self.worksheet.cell(r, c) 吗?自我不只属于类吗? xlrd 中哪些对象有 cell 属性?
    • 抱歉,复制粘贴错误,你的情况应该是 cell = sheet.cell(r,c)
    • 如果代码对您有用,请接受为正确答案,以帮助他人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多