Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for循环中嵌套的“if”逻辑错误_Python_If Statement_For Loop - Fatal编程技术网

Python for循环中嵌套的“if”逻辑错误

Python for循环中嵌套的“if”逻辑错误,python,if-statement,for-loop,Python,If Statement,For Loop,我有一个读取Excel文件的脚本,单元格A1~A6包含: OK 17 OK 9 BKK 17 OK 16 OK 12 BKK 16 它们是Excel文件的唯一内容 我要做的是检查单元格中的“OK”或“BKK”代码,并告诉我单元格中的代码是否与上面一行相同 例如,第2行有“OK”,这与第1行有“OK”相同,因此它应该告诉我“OK Find”和“第2行和第1行Find same code” 但是,在下面运行的结果会跳过一些行: from xlrd import open_workbook the

我有一个读取Excel文件的脚本,单元格A1~A6包含:

OK 17
OK 9
BKK 17
OK 16
OK 12
BKK 16
它们是Excel文件的唯一内容

我要做的是检查单元格中的“OK”或“BKK”代码,并告诉我单元格中的代码是否与上面一行相同

例如,第2行有“OK”,这与第1行有“OK”相同,因此它应该告诉我“OK Find”和“第2行和第1行Find same code”

但是,在下面运行的结果会跳过一些行:

from xlrd import open_workbook

the_file = open_workbook('c:\\file.xls',formatting_info=True)
the_sheet = the_file.sheet_by_index(0) 

for row in range(0, the_sheet.nrows):
    a = the_sheet.cell(row, 0).value
    above_a = the_sheet.cell(row-1, 0).value
    if a[0:2] == above_a[0:2]:
        print 'row no.' + str(row + 1) + ' and ' + str(row) + ' found same code'
        if 'OK' in a:
            print 'OK found'
        else:
            print 'BKK found'
结果是:

row no.2 and 1 found same code
OK found
row no.5 and 4 found same code
OK found
逻辑是错误的

澄清 有6个值需要检查,所以我希望有6个结果,但是只有4个。跳过了2个

可以这样计算,但有没有办法简化它

for row in range(0, the_sheet.nrows):
    a = the_sheet.cell(row, 0).value
    above_a = the_sheet.cell(row-1, 0).value
    if a[0:2] == above_a[0:2] and 'OK' in a:
        print 'row no.' + str(row + 1) + ' and ' + str(row) + ' found same code' + ' OK found'
    if a[0:2] == above_a[0:2] and 'BKK' in a:
        print 'row no.' + str(row + 1) + ' and ' + str(row) + ' found same code' + ' BKK found'
    if a[0:2] != above_a[0:2] and 'BKK' in a:
        print 'BKK found'

    if a[0:2] != above_a[0:2] and 'OK' in a:
        print 'OK found'

我不确定你的代码是否真的跳过了行。如果找不到匹配项,则不打印任何内容。如果将else添加到外部If循环,如下所示:

from xlrd import open_workbook

the_file = open_workbook('c:\\file.xls',formatting_info=True)
the_sheet = the_file.sheet_by_index(0) 

for row in range(0, the_sheet.nrows):
    a = the_sheet.cell(row, 0).value
    above_a = the_sheet.cell(row-1, 0).value
    if a[0:2] == above_a[0:2]:
        print 'row no.' + str(row + 1) + ' and ' + str(row) + ' found same code'
        if 'OK' in a:
            print 'OK found'
        else:
            print 'BKK found'
    else:
        print 'row no.' + str(row + 1) + ' and ' + str(row) + ' do not match'
您应该得到以下结果:

row no.1 and 0 do not match
row no.2 and 1 found same code
OK found
row no.3 and 2 do not match
row no.4 and 3 do not match
row no.5 and 4 found same code
OK found
row no.6 and 5 do not match
row no.2 and 1 found same code
OK found
row no.3 and 2 do not match
row no.4 and 3 do not match
row no.5 and 4 found same code
OK found
row no.6 and 5 do not match
更大的问题是,通过从第一行开始比较一行和上面的行,因为for循环中的范围从0到5。因此,第一个比较是OK 17和BKK 16之间的比较,即第0行和第1行。如果注释掉if循环并告诉python在for循环中打印a,在a之上,您应该能够看到这一点

就行索引而言,您比较的是以下a,在a之上:

0 -1
1 0
2 1
3 2
4 3
5 4
您可以通过从0开始并与下面的行进行比较来解决这个问题,或者更简单地说,从1开始for循环。这将为您提供以下结果:

row no.1 and 0 do not match
row no.2 and 1 found same code
OK found
row no.3 and 2 do not match
row no.4 and 3 do not match
row no.5 and 4 found same code
OK found
row no.6 and 5 do not match
row no.2 and 1 found same code
OK found
row no.3 and 2 do not match
row no.4 and 3 do not match
row no.5 and 4 found same code
OK found
row no.6 and 5 do not match
==================================================================================

要解决您的编辑问题,请执行以下操作:

for循环的第二个版本做得更好,因为它包括不匹配的情况。但您的范围仍然从0开始,因此它将第一行索引0与最后一行索引-1进行比较。这并不理想

关于简化新for循环中的if语句,可以使用elif和else而不是四个if语句。您还可以将最后两个if语句更改为一个else语句,并嵌套一个if来测试行中是否有OK或BKK。以下代码是一个示例:

for row in range(1, the_sheet.nrows):
    a = the_sheet.cell(row, 0).value
    above_a = the_sheet.cell(row-1, 0).value
    if a[0:2] == above_a[0:2] and 'OK' in a:
        print 'row no.' + str(row + 1) + ' and ' + str(row) + ' found same code' + ' OK found'
    elif a[0:2] == above_a[0:2] and 'BKK' in a:
        print 'row no.' + str(row + 1) + ' and ' + str(row) + ' found same code' + ' BKK found'
    else:
        if 'BKK' in a:
            print 'BKK found in row %d' % row
        else:
            print 'OK found in row %d' % row
还有一个问题需要解决。上面的代码只给出5个结果。听起来你想知道两件事:

单元格内容是否包含OK或BKK? 单元格的内容是否与其上方单元格的内容匹配 关于第一个问题? 你可能遇到的问题是,第一个问题涉及6个答案,而第二个问题只涉及5个。第一行上面没有一行,因此没有第二个问题的答案。您可以将代码更改为分别回答每个问题,或者将两个问题合并到一个打印语句中,该语句包含除第一行之外的每一行的比较


如果我误解了您试图回答的问题,请进一步澄清

代码根据您的描述打印正确的内容。少了什么?如果a和a以上不相等,则不打印任何内容。这就是你所说的跳过行的意思吗?另外,做第一行,眉毛,因为第一行永远不会匹配。谢谢tdelaney。有6个值需要通过,但结果只有4个。我的意思是有2个缺失…你的外部if需要一个带有打印语句的else子句。这将为每一行显示一些内容。在预览中,tdelaney说得更简洁、更快速……但是@user3684695通过一个示例完成了繁重的工作,并且知道第1行被包装到单元格的另一端,因此我认为他/她应该在这里获胜。谢谢,user3684695。非常感谢您的帮助和分析精神!