尝试通过Excel文件的每一行循环一个数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试通过Excel文件的每一行循环一个数组相关的知识,希望对你有一定的参考价值。
我试图通过数组中的每个值循环到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")
稍后编辑:更改答案以获取所有搜索的值
以上是关于尝试通过Excel文件的每一行循环一个数组的主要内容,如果未能解决你的问题,请参考以下文章