在excel工作表中搜索一个特定的字符串,该字符串的索引号为Python



这里我想在一个包含多个字符串的excel工作表中搜索,其中excel工作表包含多个索引。如果excel文件的任何一个索引包含这些字符串,那么它必须打印true。

for i in sheet_data:
if search_string= "string1" or "string2" or "string3" etc.
print true
else:
print false

我已经看过这个答案,但它会在字符串中搜索特定的单元格,但在我的情况下,字符串单元格无法预测,如果任何字符串包含在任何索引的任何单元格中,那么它必须打印true。

import xlrd
sheet_data = []   
wb = xlrd.open_workbook(Path_to_xlsx)
p = wb.sheet_names()
for y in p:
sh = wb.sheet_by_name(y)
for rownum in xrange(sh.nrows):
sheet_data.append((sh.row_values(rownum)))
found_list = []
rows_to_be_saved = []
for i in sheet_data:
if i[2] == "string1" or i[2] == "string2" or i[2] == "string3" or i[2] == "string4" or i[2] == "string5":
found_list.append(i)
else:
rows_to_be_saved.append(i)
text_file = open("Output.txt", "w")
text_file.write(found_list)
text_file.close()

您有一个数据列表,您想在其中搜索一些字符串,所以我可以向您建议以下内容。

strings = ("string", "string2", "string3")
for line in list:
if any(s in line for s in strings):
print("String Found")

最新更新