I have an Excel file with a list of shops and their addresses. I want to search it by shop number and save the address to a file. My problem is that I am a newb and I don't know how to make the loop stop and re-run input('Enter shop number: ')
when the shop number doesn't exist in my Excel file. Currently, my script prints 'Invalid shop number!' for every row. Can someone help me? I know that it's probably something basic, but as I said, I am a newbie... Thanks in advance!
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!')