Select to view content in your preferred language

set and arcpy.da.Search Cursor

322
1
11-12-2013 06:06 AM
LarryAdgate
Occasional Contributor
Except for the #local Variables, the body of this script works fine. When working with one feature class, setting a workspace seems easy, but when setting the workspace for multiple feature classes it becomes more difficult. If anyone can help-a world of thanks, Larry   

import arcpy
import sets

#local Variables:
Asbuilt_Log = "C:/Projects/Asbuilt_Log.gdb"
Rancho = "C:/Projects/North/Rancho.gdb"
Clearlake = "C:/Projects/South/Clearlake.gdb

#Body of script
set_one = set, int(r[0] for r in arcpy.da.SearchCursor("Asbuilt_Log", "GWO"))
set_two = set, int(r[0] for r in arcpy.da.SearchCursor("Rancho", "GWO"))
set_three = set, int(r[0] for r in arcpy.da.SearchCursor("Clearlake", "GWO"))                                                      
print "Items unique to Asbuilt_Log: {}".format(", ".join(sorted(set_one-set_two-set_three)))
Tags (2)
0 Kudos
1 Reply
XanderBakker
Esri Esteemed Contributor
Except for the #local Variables, the body of this script works fine. When working with one feature class, setting a workspace seems easy, but when setting the workspace for multiple feature classes it becomes more difficult. If anyone can help-a world of thanks, Larry   

import arcpy
import sets

#local Variables:
Asbuilt_Log = "C:/Projects/Asbuilt_Log.gdb"
Rancho = "C:/Projects/North/Rancho.gdb"
Clearlake = "C:/Projects/South/Clearlake.gdb

#Body of script
set_one = set, int(r[0] for r in arcpy.da.SearchCursor("Asbuilt_Log", "GWO"))
set_two = set, int(r[0] for r in arcpy.da.SearchCursor("Rancho", "GWO"))
set_three = set, int(r[0] for r in arcpy.da.SearchCursor("Clearlake", "GWO"))                                                      
print "Items unique to Asbuilt_Log: {}".format(", ".join(sorted(set_one-set_two-set_three)))


Hi Larry,

There's nothing wrong with working with more than 1 workspace. In fact you don't have to define a workspace (arcpy.env.workspace) in your script.

There are a few errors in the script.

  • The local variables should point to featureclass (or tables), now they point to file geodatabases

  • You don't use the local variables in the search cursors. In stead you use strings, which arcpy will search for in the current workspace (and will probably not find).

  • the set, int(... statement is not OK

Try this:

import arcpy
import sets

#local Variables:
Asbuilt_Log = "C:/Projects/Asbuilt_Log.gdb/NameOfFeatureClassHere"   # edit this
Rancho = "C:/Projects/North/Rancho.gdb/NameOfFeatureClassHere"       # edit this
Clearlake = "C:/Projects/South/Clearlake.gdb/NameOfFeatureClassHere" # edit this

fldName = "YourFieldToCompare" # edit this

#Body of script
set_one = set(int(r[0]) for r in arcpy.da.SearchCursor(Asbuilt_Log, fldName))
set_two = set(int(r[0]) for r in arcpy.da.SearchCursor(Rancho, fldName))
set_three = set(int(r[0]) for r in arcpy.da.SearchCursor(Clearlake, fldName))
print "Items unique to Asbuilt_Log: {}".format(", ".join(sorted(set_one-set_two-set_three)))



Some more reading: SearchCursor (arcpy.da)

Kind regards,

Xander
0 Kudos