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\Desktop10円X_Testing_databases\SDE_TEST.gdb\RoadCenterline_test"
Source_Layer = r"C:\Users\craig\Desktop10円X_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\Desktop10円X_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\Desktop10円X_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\Desktop10円X_Testing_databases\SDE_TEST.gdb\RoadCenterline_sample"
Target_SDE_FC = r"C:\Users\craig\Desktop10円X_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)
-
1Please do not edit an answer into your question. If you feel the suggested answers do not suffice, post an answer yourself and accept it following the waiting period.jpmc26– jpmc262014年06月02日 20:50:14 +00:00Commented Jun 2, 2014 at 20:50
-
that's not what has been suggested in the past.Craig– Craig2014年06月02日 20:52:17 +00:00Commented Jun 2, 2014 at 20:52
-
I think it is somewhat debatable but nowadays I would probably opt for adding a separate Answer rather than including an answer in a Question. There is nothing stopping you from leaving Jason's Answer as the accepted one above which your may or may not float over time. The Accept button is there for you to choose how you want to reward the efforts that go into answering your Questions. Feel free to discuss any nuances of how GIS SE works like this in Meta.PolyGeo– PolyGeo ♦2014年06月02日 21:44:37 +00:00Commented Jun 2, 2014 at 21:44
-
1@Craig gis.stackexchange.com/help/self-answer, blog.stackoverflow.com/2011/07/…, This touches on putting an answer in the question as a possible option that other users recommend against: meta.stackexchange.com/questions/17845/…. Maybe I'm assuming too much coming from StackOverflow; it's frowned upon there. But I tend to think it makes sense that questions go in the question section and answers go in the answer section. =) Like Poly says, there's meta if there are still questions/issues.jpmc26– jpmc262014年06月03日 01:02:57 +00:00Commented Jun 3, 2014 at 1:02
1 Answer 1
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\Desktop10円X_Testing_databases\SDE_TEST.gdb\RoadCenterline_sample"
Target_SDE_FC = r"C:\Users\craig\Desktop10円X_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()