I'm trying to find a similar record between two data sets in a dictionary with which to do further comparisons.
I've confirmed with a print statement that it is finding a matching data set (so all of the code before the final if statement is working). However it is not setting the matchingSet2Record
for some reason. This causes the final if statement to always run even though it is finding a match. Declaring the variable as being in the global variable scope does not work. What is causing this to happen? How do I set the first mathingSet2Record
to the discovered record in the for loop?
The only problem I'm having with this code is that even though matchingSet2Record
is set to the found record properly, it still has a value of None
when trying to compare it in the final if statement. The comparison logic is functioning properly.
I have the following function:
def processFile(data):
# Go through every Record
for set1Record in data["Set1"]:
value1 = set1Record["Field1"].strip()
matchingSet2Record = None
# Find the EnergyIP record with the meter number
for set2Record in data["Set2"]:
if set2Record["Field2"].strip() == value1:
global matchingSet2Record
matchingSet2Record = set2Record
# If there was no matching Set2 record, report it
if matchingSet2Record == None:
print "Missing"
Updated code per answers/comments (still exhibiting the same issue)
def processFile(data):
# Go through every Record
for set1Record in data["Set1"]:
value1 = set1Record["Field1"].strip()
matchingSet2Record = None
# Find the EnergyIP record with the meter number
for set2Record in data["Set2"]:
if set2Record["Field2"].strip() == value1:
matchingSet2Record = set2Record
# If there was no matching Set2 record, report it
if matchingSet2Record == None:
print "Missing"
"data" is a dictionary of dictionaries. That portion of the code is working properly. When I print matchingSet2Record within the for loop that set's it to the matching record it shows that the variable was set properly, however when I do it outside of the for loop it shows a value of None. That is the problem that I'm exploring with this code. The problem does not have anything to do with the code that finds a matching record.