I am trying to loop over a file and find if an entry exists. I need to search through a date range and am not sure whether to loop over the lines in the file first and then each date, or to loop over the dates and then look in each line?
I have tried both options, but the code below seems more 'logical'. My question is how does one reason this out like a programmer? And why does the code below not try all single_date
s, but only iterate once through all the lines in the file.
with open(r'reportLog.txt','r') as logFile:
for single_date in daterange(start_date, end_date):
for line in logFile:
if all(var in line for var in (reportName, str(single_date), 'R')):
print('found')
break
else:
print('not found')
reportLog.txt:
Digital_Incomplete_Leads,2019-05-10,12:15:29,12:15:29,Y
Digital_Incomplete_Leads,2019-05-09,12:15:43,12:15:43,Y
Account Movement Report,2019-05-06,13:54:07,13:54:12,Y
Account Movement Report,2019-05-07,13:54:07,13:54:12,Y
Account Movement Report,2019-05-08,13:54:07,13:54:12,Y
Account Movement Report,2019-05-09,13:53:38,13:53:38,R
Account Movement Report,2019-05-09,13:54:07,13:54:12,Y
I want the code to loop over the text file and exit when it finds the following line:
Account Movement Report,2019-05-09,13:53:38,13:53:38,R