AnsweredAssumed Answered

Update Cursor script that allows selectable field to be updated?

Question asked by rralbritton on Jun 6, 2013
Latest reply on Jun 6, 2013 by thanos_cp78
I've written a code that selects attributes one by one. As each attribute is selected a select by location is performed to identify how many records in the select by location file intersect with the selected record from the select by attributes query. The number of records that intersect the selected attribute are recorded to the attribute table. The script works if the field to be updated is hardcodded into the script. I want to be able to pick what field gets to be updated each time the script is run. Is this possible?

import arcpy, os  arcpy.env.workspace = "C:/Temp" #arcpy.GetParameterAsText(0) arcpy.env.overwriteOutput = True outputWorkspace = "C:/Temp/Scratch" #arcpy.GetParameterAsText(1)  def outName(input,post="_Output"):     """Returns output name."""     outName=os.path.basename(input).split(".")[0]+post     return outName      Attribute = "Parcel_Test.shp" #arcpy.GetParameterAsText(2) Location = "Test_Wetlands.shp" #arcpy.GetParameterAsText(3)       #Get Count of the number of records for the file needing to be updated. #This count will be automatically plugged into a range value needed to write results from the analysis to the output shapefile. RangeCount = int(arcpy.GetCount_management(Attribute).getOutput(0)) print "There are", RangeCount, "records that need to be updated" arcpy.AddMessage("There are "+str(RangeCount)+" that need to be updated\n")  #Create Features Layers from Feature Classes AttributeFL=outName(Attribute,"_Layer") LocationFL=outName(Location,"_Layer") arcpy.MakeFeatureLayer_management(Attribute,AttributeFL) arcpy.MakeFeatureLayer_management(Location, LocationFL)  #Begin Update print "\nUpdating file with the number of records that intersect with each attribute" arcpy.AddMessage("Updating file with the number of records that intersect with each attribute\n")  for attribute in range(0,RangeCount):     FID = "FID=%s" % (attribute)     arcpy.SelectLayerByAttribute_management(AttributeFL,"NEW_SELECTION",FID)     arcpy.SelectLayerByLocation_management(LocationFL,"INTERSECT",AttributeFL)     LocationCount = int(arcpy.GetCount_management(LocationFL).getOutput(0))     arcpy.AddMessage(str(FID)+" has "+str(LocationCount)+" records that intersect with it.")     print str(FID), "has", LocationCount, "records that intersect with it."          #Update the three_mi field      uc=arcpy.UpdateCursor(AttributeFL)     for line in uc:         line.count = LocationCount         uc.updateRow(line) #Actually changes the table values to buffer count     del line     del uc      arcpy.SelectLayerByAttribute_management(AttributeFL,"CLEAR_SELECTION")      arcpy.AddMessage(Attribute+" has been updated\n") print Attribute, "has been updated\n"

Outcomes