I am trying to compare two fields in my attribute table. I did a spatial join with a point feature and the closest address. Not all of the addresses matched up correctly so for the point features that have address data, I want to compare just the numbers in that field to another field where I separated the number from the address that was joined. I need to locate all the numbers that do not match up. I am not very experienced with python, so I am trying to use copilot to generate something. Here is what it is giving me:
import re
def compare_fields(Add_Number, LocationDescription):
# Extract numbers from the LocationDescription field
address_numbers = re.findall(r'\d+', LocationDescription)
# Convert the extracted numbers to a single string for comparison
address_number_str = ''.join(address_numbers)
# Compare the Add_Number field with the extracted address numbers
if str(Add_Number) == address_number_str:
return "Match"
else:
return "No Match"
I am putting this script in the field calculator for a field where I want Match and No Match to be stored. It needs to compare the two fields LocationDescription (This is the script with the full address. These addresses are all in different formats. Some have the number at the end or contain underscores) and Add_Number (just the numbers for the joined address). I also tried a script that would extract the numbers only from LocationDescription because I thought it would be easier to compare if both fields were just numbers, but it also did not work. Does anyone have any idea what I am missing with this? This would help me a ton if someone could help me out otherwise, I will have to manually go through over 15,000 records.
The following may help but you may have to modify it as needed.
Btw start by removing import re
In Calculate Field
def reclass(Add_Number):
if(str(Add_Number) == !Add_Number_str!):
return "Match"
else:
return "No Match"