我试图通过数组中的每个值循环到Excel文件的每一行。我希望第一个匹配打印“找到数组值”,如果找不到匹配我要打印“未找到数组值”
如果大多数工作,除了我想在发现匹配时突破循环而它没有。
使用lxrd
for row_num in range(sheet.nrows):
for n in name_array:
row_vlaue = sheet.row_values(row_num)
if row_vlaue[4] == r in name_array:
print(n, "found")
break
else:
print(n, "not found")
从您的代码看,您似乎只在第5个单元格(row_vlaue[4]
)上搜索值,如果这是真的,那么这应该足够了:
for row_num in range(sheet.nrows):
row_vlaue = sheet.row_values(row_num)
if row_vlaue[4] in name_array: # this checks if the 5th cell is in your list of searched values
print(row_vlaue[4], "found on row", row_num)
name_array.remove(row_vlaue[4]) # we remove the element we've found
if not name_array: # nothing else to search for
print("found them all")
break
else:
# this else will be executed only if the break was not called
# this means that there still something left in in list of searched values
print(name_array, "not found")
稍后编辑:更改答案以获取所有搜索的值