[GIS] Looping through List With ArcPy Cursor

arcgis-10.1arcpycursor

My team is working on a python script that does various updating tasks to our streets file. A part we are getting stuck on is trying to update a field.

Currently we have a field FEDROUTE that is populated with federal, local, state route numbers. We want to loop through a list of federal route numbers and if the search cursor finds a number in the list, leave it alone, however if it finds a number that isn't within the federal route list, then delete the value in that field, leaving it blank.

Right now the script will run through the first value in the list but won't start over and go through the remaining values in the list.

The code we have written so far is:

import arcpy
import os
import StringIO
import datetime

Target_SDE_FC = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb\RoadCenterline_test"
Source_Layer = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb\RoadCenterline_sample"
SL_Layer = "CreateSource_Layer"
arcpy.DeleteFeatures_management(Target_SDE_FC)

print "Target_SDE_FC features deleted"

arcpy.MakeFeatureLayer_management(Source_Layer, SL_Layer, "", arcpy.env.scratchWorkspace)

fms = arcpy.FieldMappings()
fms.addTable(Target_SDE_FC)

#fedRts = ("75","441","41","301","27","21")
fedRts = ("21","27","301","41","441","75")

# Define the output layer for the append
arcpy.env.workspace = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb"
outFC = "RoadCenterline_test"

# Try block to Append Feature Layer to the SDE (Test Data) with field mappings
try:
    arcpy.Append_management(SL_Layer,outFC,"NO_TEST",fms)
#    test_output.close()
finally:
#    outFC.close()
    print "append finished from try loop"


#arcpy.env.workspace = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb"
#sde = "RoadCenterline_test"

print "start cursor"

#create update cursors to delete the records not needed in Federal, State, and County Routes
try:
    for i in (fedRts):
        print "reading a new i value"
        with arcpy.da.UpdateCursor(outFC, "FEDROUTE") as fCursor:
            for record in fCursor:
                print "start cursor"
                if record == i:
                    print "record is not i"
                    break
                elif record == "":
                    print "record is blank"
                    break
                else:
                    print "read a new record from cursor for same i"
            fCursor.deleteRow()
        break



finally:
    del record
    del fCursor
    del SL_Layer
    print "script finished"

@JasonScheirer’s answer helped clean up our code considerably. To achieve what we wanted to do we wound up with:

import os
import arcpy

fedRts = ("21","27","301","41","441","75")
original_fc = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb\RoadCenterline_sample"
Target_SDE_FC = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb\RoadCenterline_test"

arcpy.Append_management(original_fc, Target_SDE_FC, "NO_TEST")
newValue = ""

with arcpy.da.UpdateCursor(Target_SDE_FC, "FEDROUTE") as fCursor:
    for record in fCursor:
        print "start cursor"
        if record[0] not in fedRts:
            record[0] = newValue
            fCursor.updateRow(record)

Best Answer

You've got quite a bit of superfluous code in there. You don't need to make a layer from the feature class, each row returns a list (so use record[0]), and to test for membership you can use the in operator. Pretty sure this bit of code is all you need:

import os

import arcpy

fedRts = ("21","27","301","41","441","75")
original_fc = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb\RoadCenterline_sample"
Target_SDE_FC = r"C:\Users\craig\Desktop\10X_Testing_databases\SDE_TEST.gdb\RoadCenterline_test"

arcpy.Append_management(original_fc, Target_SDE_FC, "NO_TEST")

with arcpy.da.UpdateCursor(new_fc, "FEDROUTE") as fCursor:
    for record in fCursor:
        print "start cursor"
        if record[0] not in fedRts:
            fCursor.deleteRow()