我有一个脚本来读取Excel文件,单元格A1~A6包含:
OK 17
OK 9
BKK 17
OK 16
OK 12
BKK 16
它们是Excel文件的唯一内容。
我想要做的是检查单元格中的“OK”或“BKK”代码,并告诉我单元格中的代码是否与上面的那一行相同。
例如,第2行有'OK',第1行有'OK',所以它会告诉我'OK found'和'row no.2和1找到相同的代码'。
但是下面运行的结果会跳过一些行:
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个被跳过。
可以这样解决但有没有办法简化它?
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'
答案 0 :(得分:2)
我不确定您的代码是否实际跳过了行。如果找不到匹配项,您就不会打印任何内容。如果您在外部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
更大的问题是你是通过从第一行开始比较一行和上面的行(因为for循环中的范围从0到5)。所以第一次比较是在" OK 17"和" BKK 16" (即第0行和第-1行)。如果您注释掉if
循环并在print a, above_a
循环中告诉python到for
,您应该能看到这个。
for row in range(0, the_sheet.nrows):
a = the_sheet.cell(row, 0).value
above_a = the_sheet.cell(row-1, 0).value
print a, above_a
就行索引而言,您正在比较以下(a,above_a):
0 -1
1 0
2 1
3 2
4 3
5 4
您可以通过从0开始并与下面的行进行比较来解决此问题,或者更简单地说,在1处开始for
循环。这会给您以下结果:
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个结果。听起来你想知道两件事:
您可能遇到的问题是,第一个问题涉及6个答案,但第二个问题仅涉及5.第一行没有一行,因此没有答案第二个问题。您可以更改代码以单独回答每个问题,或者将两个问题组合成单个打印语句,其中包括每行但第一行的比较。
如果我误解了您尝试回答的问题,请进一步澄清。