I have two files. The first file contains a list of 6 character keys (SA0001, SA1001, etc.). The second file contains a list of dates and amounts where the first six positions will match the key in the first file. I want to verify that every key in the first file has at least one match in the second file. There may be more than one match which is okay and there may be records in the second file with no key in the first file which is also okay. So basically a loop within a loop. The problem arises when I want to break out of the inner loop after the first match because the second file could be quite large. It prints out the "found" message correctly and breaks, but it won't print the "not found" message if it reaches the end of the second file with out finding a match. My code so far is:
unvalues = open("file1.txt", "r")
newfunds = open("file2.txt", "r").readlines()
i = 1
for line in newfunds:
line = line.strip()
for line2 in iter(unvalues.readline, ""):
try:
if line == line2[:6]:
print "%s: Matching %s to %s for date %s" % (i, line, line2[:6], line2[6:14])
break
except StopIteration: print "%s: No match for %s" % (i, line)
i += 1
unvalues.seek(0)