如何检查单元格是否包含值

时间:2019-07-29 13:17:59

标签: python excel xlrd

我有一个代码,用于检查给定单元格值的位置(哪个单元格)。如果该单元格包含任何值(因此它不为null),如何检查该单元格下每个单元格长4行(因此在新行中)?我启动了它,但不知道如何继续。

for row in range(rows):
    for col in range(cols):
        if thecell.value == "Table1":

1 个答案:

答案 0 :(得分:1)

enumerate()可能会有所帮助。

示例:

for y, row in enumerate(range(rows)):
    for x, col in enumerate(range(cols)):
        #making this a 2d array for the example
        if thecell[y][x] == "Table1":
            for i in range(y + 1, y + 4):
                if not thecell[i][x] == None:
                    #Do whatever you need to do when the cell is NOT none
                    print("CELL EMPTY AT:  " + str(x) + "," + str(y))

免责声明:我对xlrd一无所知。但是,如果您向我提供更多代码,我可能会提供更多帮助。就像您在哪里获得行/范围?像那样的东西。甚至可能有一个内置方法来检查单元格是否为空白(因为在excel中为ISBLANK())。

如果您复制并粘贴此代码,肯定不会起作用,但是至少应该给您提供一两个解决问题的方法。让我知道此解决方案是否有效,或者您是否需要更多帮助。