在 Excel 文件中搜索特定值时如何跳出 Python 中的嵌套循环?
How to break out of nested loops in Python when searching for a specific value in an Excel file?
我有一个 Excel 文件,其中包含商店及其地址的列表。我想按商店编号搜索它并将地址保存到文件中。我的问题是我是新手,当我的 Excel 文件中不存在商店编号时,我不知道如何停止循环并重新 运行 input('Enter shop number: ')
。目前,我的脚本为每一行打印 'Invalid shop number!' 。有人能帮我吗?我知道这可能是一些基本的东西,但正如我所说,我是新手......提前致谢!
import openpyxl
from openpyxl import Workbook
file = '...\Desktop\shops.xlsx'
wb = openpyxl.load_workbook(file, read_only=True)
ws = wb.active
shop = int(input('Enter shop number: '))
save = open('shop.txt', 'w')
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
else:
print('Invalid shop number!')
您可以使用布尔标志来指示是否找到商店编号:
found = False
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
found = True
if not found:
print('Invalid shop number!')
如果您只想处理找到的第一个带有店号的单元格或没有重复的店号,您可以将其放在一个函数中,然后 return 找到店号后。
或者,如果标志是 True
或 continue
,则可以在 if
语句中中断并在内部循环之后再次中断 else
子句内部循环和 break
在该子句之后。
见the answers to this related question about how to break out of multiple loops。
您必须将逻辑放在 while 循环中,当您在文件中找到商店地址时或根据您的要求从 while 循环中获取输入并中断 while 循环。
save = open('shop.txt', 'w')
while not found:
shop = int(input('Enter shop number: '))
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
found = True
# or you can write
# break
if found:
break
if not found:
print('Invalid shop number!')
这行得通!谢谢大家的帮助!
save = open('shop.txt', 'w')
while not found:
shop = int(input('Enter shop number: '))
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
found = true
break
if not found:
print('Invalid shop number!')
我有一个 Excel 文件,其中包含商店及其地址的列表。我想按商店编号搜索它并将地址保存到文件中。我的问题是我是新手,当我的 Excel 文件中不存在商店编号时,我不知道如何停止循环并重新 运行 input('Enter shop number: ')
。目前,我的脚本为每一行打印 'Invalid shop number!' 。有人能帮我吗?我知道这可能是一些基本的东西,但正如我所说,我是新手......提前致谢!
import openpyxl
from openpyxl import Workbook
file = '...\Desktop\shops.xlsx'
wb = openpyxl.load_workbook(file, read_only=True)
ws = wb.active
shop = int(input('Enter shop number: '))
save = open('shop.txt', 'w')
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
else:
print('Invalid shop number!')
您可以使用布尔标志来指示是否找到商店编号:
found = False
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
found = True
if not found:
print('Invalid shop number!')
如果您只想处理找到的第一个带有店号的单元格或没有重复的店号,您可以将其放在一个函数中,然后 return 找到店号后。
或者,如果标志是 True
或 continue
,则可以在 if
语句中中断并在内部循环之后再次中断 else
子句内部循环和 break
在该子句之后。
见the answers to this related question about how to break out of multiple loops。
您必须将逻辑放在 while 循环中,当您在文件中找到商店地址时或根据您的要求从 while 循环中获取输入并中断 while 循环。
save = open('shop.txt', 'w')
while not found:
shop = int(input('Enter shop number: '))
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
found = True
# or you can write
# break
if found:
break
if not found:
print('Invalid shop number!')
这行得通!谢谢大家的帮助!
save = open('shop.txt', 'w')
while not found:
shop = int(input('Enter shop number: '))
for row in ws.iter_rows(1):
for cell in row:
if cell.value == shop:
print(ws.cell(row=cell.row, column=2).value, file = save)
save.close()
found = true
break
if not found:
print('Invalid shop number!')