searchcursor to compare records

3277
4
Jump to solution
03-22-2016 07:42 AM
BrianLomas
Occasional Contributor III

I'm new to python so forgive me if this seems like an easy question... I want to compare fields of an mdb and a FC to identify inconsistencies. Also, the field names do not match. So, I have a script using a searchcursor and lists. Right now the script runs but it only seems to run on the first field and I need it to loop through 12 total. Any ideas on how to keep the loop running through all the fields?

from time import strftime  
   
print "Start script: " + strftime("%Y-%m-%d %H:%M:%S")  
   
import arcpy  
   
sourceFC = "M:\...\January2015Ownership"
   
sourceFieldsList = ["PARCEL_","SITUS", "CURRENT_OWNER", "CO_OWNER", "OWNER_MAILADDR", "OWNER_CITY_STATE", "OWNER_ZIPCODE",  "TAX_DISTRICT", "TAX_CODE", "SEC", "TOWNSHIP", "RANGE", "LEGAL"]  
   
# Use list comprehension to build a dictionary from a da SearchCursor  
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}  
   
updateFC = "M:\...\TaxParcel_COPY"
   
updateFieldsList = ['PARCELID','SITEADDRESS','OWNERNME1', 'OWNERNME2', 'PSTLADDRESS', 'PSTLCITY', 'ZIPCODE', 'TAX_DIST',  'TAX_CODE', 'SEC', 'TWN', 'RGE', 'PRPRTYDSCRP']  
   

with arcpy.da.SearchCursor(updateFC, updateFieldsList) as originalRows:  
        for originalRow in originalRows:
            # store the Join value of the row being original in a keyValue variable  
            keyValue = originalRow[0]  
            # verify that the keyValue is in the Dictionary  
            if keyValue in valueDict:
                   for n in range (1,len(sourceFieldsList)):  
                        if originalRow != valueDict[keyValue][n-1]:
                            print(str(keyValue) + ' was Changed in field "' + sourceFieldsList + '"')
                            break 
                       
            else:  
                print(str(keyValue) + ' check')
                break  
                      
del valueDict  
   
print "Finished script: " + strftime("%Y-%m-%d %H:%M:%S")
0 Kudos
1 Solution

Accepted Solutions
BrianLomas
Occasional Contributor III

Well, I knew it had to be something easy. I removed the "break" after the if and else statement and it seems to run on all the fields now. I still have a little work to do on the script but I appreciate the help and fast responses.

View solution in original post

0 Kudos
4 Replies
AdrianWelsh
MVP Notable Contributor

Hi Brian,

It looks like it may be an issue with your text string for updateFC. Try putting in an "r" at the beginning of it.

updateFC = r'M:\BrianLomas\Python Scripts\ParcelPracticeCopies\ParcelPythonCOPIES.gdb\TaxParcel_COPY'

See this thread/poll on python naming string conventions:

0 Kudos
BrianLomas
Occasional Contributor III

After changing the path names to raw path strings, I am still getting the same issue. The script will run but it seems to only go through the first field when I know there are more inconsistencies in other fields. I think the issue might be in the loop.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

In fact, both feature classes should use raw notation (ie line 7)

0 Kudos
BrianLomas
Occasional Contributor III

Well, I knew it had to be something easy. I removed the "break" after the if and else statement and it seems to run on all the fields now. I still have a little work to do on the script but I appreciate the help and fast responses.

0 Kudos