# Set the variables mxd = arcpy.mapping.MapDocument(r"\\working\BioFire.mxd") df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0] lyr = arcpy.mapping.ListLayers(mxd,"Fire",df)[0] # Loop through specified records in layer ### I CAN'T FIGURE OUT THE CODE FOR THIS NEXT BIT cur = arcpy.SearchCursor(lyr, '"FIRECODE" in ("ARC", "CEA", "DAB", "MGD")') ### IF I USE THE LINE BELOW INSTEAD IT WORKS FINE BUT LOOPS THROUGH ALL RECORDS WHICH I DON'T WANT #cur = arcpy.SearchCursor(lyr) . . . .
Solved! Go to Solution.
SQLexpression = "FIRECODE in ('ARC', 'CEA', 'DAB', 'MGD')" cur = arcpy.SearchCursor(lyr,sQLexpression)
SQLexpression = ''' "FIRECODE" in ('ARC', 'CEA', 'DAB', 'MGD') ''' cur = arcpy.SearchCursor(lyr,sQLexpression)
SQLexpression = "FIRECODE in ('ARC', 'CEA', 'DAB', 'MGD')" cur = arcpy.SearchCursor(lyr,sQLexpression)
SQLexpression = ''' "FIRECODE" in ('ARC', 'CEA', 'DAB', 'MGD') ''' cur = arcpy.SearchCursor(lyr,sQLexpression)
fldname = "FIRECODE" lst_values = ["ARC", "CEA", "DAB", "MGD"] where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr, fldname), "','".join(lst_values)) cur = arcpy.SearchCursor(lyr, where)
print arcpy.AddFieldDelimiters(lyr, fldname) # >> "FIRECODE"
print "','".join(lst_values) # >> ARC','CEA','DAB','MGD
print "('{0}')".format("','".join(lst_values)) # >> ('ARC','CEA','DAB','MGD')
where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr, fldname), "','".join(lst_values)) print where # >> "FIRECODE" in ('ARC','CEA','DAB','MGD')
datasetname = lyr.dataSource #this is a shapefile fldname = "FIRECODE" lst_values = ["ARC", "CEA", "DAB", "MGD"] where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr1, fldname), "','".join(lst_values)) with arcpy.da.SearchCursor (datasetname,fldname,where) as cur: for eachrecord in cur: regcode = eachrecord.getValue(fldname) . .
Dear Kim and Xander, thank you very much for your responses, both worked a treat. I wish the ESRI online help was that clear and succinct.
Xander, I'm afraid you've started something. I do have 10.1 (10.2 actually), and I tried using arcpy.da.SearchCursor as you suggested. I followed the esri online help and examples as best I could and ended up with this:datasetname = lyr.dataSource #this is a shapefile fldname = "FIRECODE" lst_values = ["ARC", "CEA", "DAB", "MGD"] where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr1, fldname), "','".join(lst_values)) with arcpy.da.SearchCursor (datasetname,fldname,where) as cur: for eachrecord in cur: regcode = eachrecord.getValue(fldname) . .
At this point I get the error message "AttributeError: 'tuple' object has no attribute 'getValue'"
Any suggestions? Cheers, Damian
datasetname = lyr.dataSource fldname = "FIRECODE" lst_values = ["ARC", "CEA", "DAB", "MGD"] where = "{0} in ('{1}')".format(arcpy.AddFieldDelimiters(lyr1, fldname), "','".join(lst_values)) # define a tuple (or list) of field names to use in the cursor flds = (fldname, 'someOtherFieldname', 'andYetAnotherFieldName') with arcpy.da.SearchCursor(datasetname, flds, where) as cur: for eachrecord in cur: regcode = eachrecord[0] # firecode someOtherValue = eachrecord[1] andYetAnotherValue = eachrecord[2]
Hi, I'm trying to use a SearchCursor to loop through specific records in a layer in an ArcMap project. I would like loop through specific records based on a text field in the layer but I'm having trouble figuring out the syntax. Can anyone help please? Code below.# Set the variables mxd = arcpy.mapping.MapDocument(r"\\working\BioFire.mxd") df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0] lyr = arcpy.mapping.ListLayers(mxd,"Fire",df)[0] # Loop through specified records in layer ### I CAN'T FIGURE OUT THE CODE FOR THIS NEXT BIT cur = arcpy.SearchCursor(lyr, '"FIRECODE" in ("ARC", "CEA", "DAB", "MGD")') ### IF I USE THE LINE BELOW INSTEAD IT WORKS FINE BUT LOOPS THROUGH ALL RECORDS WHICH I DON'T WANT #cur = arcpy.SearchCursor(lyr) . . . .
Cheers, Damian