I'm trying to update a python script to use the arcpy.da.SearchCursor instead of the legacy arcpy.SearchCursor. The cursor searches for duplicate values in a field.

I am getting an error:

Traceback messages below.
AttributeError: 'tuple' object has no attribute 'getValue'

There must be a syntax difference between the two cursors that I have wrong. Can anyone tell where I am going wrong?

#Constant Vars
dblayerprefix = "CityworksGIS_Test.DBO."
whydrant = dblayerprefix + "WHYDRANT"
centralDB ="<YOUR DB CONNECTION>" #this variable used for replication
editDB ="Database Connections/DBO@CityworksGIS_Test.sde"
columnName = 'AssetID'
updateSql = "AssetID IS NULL"
noNullSql = "AssetID IS NOT NULL"
#old variable for arcpy.searchcursor
fieldascend = columnName + " A"

    #Set editor environment workspace
    arcpy.env.workspace = 'Database Connections/DBO@CityworksGIS_Test.sde'

    #Old cursor:
    #rows = arcpy.SearchCursor('CityworksGIS_Test.dbo.WHYDRANT', noNullSql ,"","", fieldascend)
    #New cursor
    rows = arcpy.da.SearchCursor('CityworksGIS_Test.dbo.WHYDRANT', columnName, noNullSql, sql_clause=(None, 'ORDER BY ASSETID ASC'))
    dupValuesList = [] #List for holding empty values
    print 'Checking WHYDRANT for duplicate SampleID values'
    i = -1
    row = rows.next()
    value = row.getValue(columnName)
    for row in rows:
        if row.getValue(columnName) != value:
            value = row.getValue(columnName)

    #Check duplicates and create report
    if len(dupValuesList) == 0:   
        print 'No duplicates found moving on'  
        del rows
        del dupValuesList
        raise ValueError('Duplicate AssetID values found in WHYDRANT')
  • Go back and re-read the documentation for arcpy.da.SearchCursor() as it appears you have updated that line, but the rest of the cursor lines are in the old arcpy.SearchCursor() format. – Midavalo Oct 28 '16 at 17:37

If I understand what your original code was doing, you are looking for duplicate ID values in your AssetID column. This modification of your Search Cursor will create a python Set to store all AssetID values, and before it stores each one it will check if the ID already exists in the set. If it does exist then it's a duplicate, and so is written to the dupValuesList List.

#New cursor
dupValuesList = [] # List for holding duplicated IDs
assetIDSet = set() # Set to record all IDs
print 'Checking WHYDRANT for duplicate AssetID values'
with arcpy.da.SearchCursor('CityworksGIS_Test.dbo.WHYDRANT', columnName, noNullSql) as cursor:
    for row in cursor:
        if row[0] in assetIDSet:

#Check duplicates and create report
  • That is exactly what the original code was doing! I have been reviewing the documentation on cursors and I'm still having difficulty (I'm pretty new to python) I ran the code you suggested and got this error Traceback messages below. AttributeError: 'set' object has no attribute 'append' – Cort Wilson Oct 28 '16 at 18:05
  • @CortWilson sorry I used append instead of add. I have updated the code above. – Midavalo Oct 28 '16 at 18:07
  • that worked perfectly! Thanks for the lesson/help! – Cort Wilson Oct 28 '16 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.