I incorporated the following code into my script from Comparing field values of two feature classes using ArcPy? and it works great but I also need to compare two other fields values (i.e. YEAR & MONTH) from the same two FC's.
There is no problem with the code the way it is, I just need help on modifying the code to check 3 fields' attributes from 2 FC's to see if each row for the three fields contain the same attributes and if so not to copy the row to the target feature class. The fields in each FC do match exactly.
import arcpy
#input feature class
fc2 = r"C:\temp\temp.gdb\fc2"
#target feature class
fc3 = r"C:\temp\temp.gdb\fc3"
#check field
checkField = "SRNumber"
#Get list of values in field from target feature class
checkValues = [r[0] for r in arcpy.da.SearchCursor (fc3, checkField)]
#Get list of fields
fields = [f.name for f in arcpy.ListFields (fc2)]
#Get index of check field
index = fields.index (checkField)
#Create insert cursor for fc3 to allow appending of rows
inCursor = arcpy.da.InsertCursor (fc3, fields)
#Create search cursor to iterate input feature class
cursor = arcpy.da.SearchCursor (fc2, fields)
#iterate
for row in cursor:
#Get value to check
checkValue = row[index]
#Check if value is in target fc
if checkValue in checkValues:
#skip if value is in target fc
continue
#Insert row otherwise
inCursor.insertRow(row)
#Clean up
del cursor
del inCursor
-
Welcome to GIS SE! As a new user be sure to take the Tour to learn about our focussed Q&A format. Which "past post" did you use code from? At the moment you are presenting what I think would be more than a code snippet.PolyGeo– PolyGeo ♦2018年07月25日 22:32:35 +00:00Commented Jul 25, 2018 at 22:32
-
1So where's the problem? Are you getting any error message? Is the output not what you require? As you are using arcpy.da.SearchCursor and InsertCursor consider using a with block (with arcpy.da.InsertCursor(fc3,fields) as inCur:) to clean up. From what I can see though you're trying to insert a row from one feature class cursor into another feature class cursor which probably wont work so good if the fields don't exactly match (including field types).Michael Stimson– Michael Stimson2018年07月25日 23:51:13 +00:00Commented Jul 25, 2018 at 23:51
-
Apologies, I have updated the post with more contextWilliam– William2018年07月26日 15:21:19 +00:00Commented Jul 26, 2018 at 15:21
1 Answer 1
To check all three attributes (or any number of listed fields) and insert complete row including geometry:
import arcpy
from operator import itemgetter
fc2 = r"C:\temp\temp.gdb\fc2" #input feature class
fc3 = r"C:\temp\temp.gdb\fc3" #target feature class
checkfields = ["SRNumber","YEAR","MONTH"] #fields to compare
fieldlist = [f.name for f in arcpy.ListFields(fc2) if not f.name.upper().startswith(('OBJ','SHA'))] #list all fields but objectid and shapefield(s)
fieldlist.append('SHAPE@')
checkindexes = [fieldlist.index(i) for i in checkfields]
all_values = [i for i in arcpy.da.SearchCursor(fc3, checkfields)]
icur = arcpy.da.InsertCursor(fc3,fieldlist)
with arcpy.da.SearchCursor(fc2,fieldlist) as cursor:
for row in cursor:
if itemgetter(*checkindexes)(row) not in all_values:
icur.insertRow(row)
del icur
-
It partially works in that it populates the target FC with those 3 fields and their attributes but it leaves all the other fields from the input FC as null and does not carry over the geometry. I thought changing the insert cursor argument to icur = arcpy.da.InsertCursor(fc3,"*") would solve the issue but it then gives me an error on the icur.insertRow(row) TypeError: sequence size must match the size of the rowWilliam– William2018年07月30日 21:36:03 +00:00Commented Jul 30, 2018 at 21:36
-
@William that was not clear by reading your question. Try again, i have updated the code. I have no suitable test data so you might get an error, then comment again.Bera– Bera2018年07月31日 17:00:02 +00:00Commented Jul 31, 2018 at 17:00
-
I did get an error on running the updated code if itemgetter(*checkindexes)(row) not in all_values: IndexError: tuple index out of rangeWilliam– William2018年07月31日 19:41:39 +00:00Commented Jul 31, 2018 at 19:41
-
@William try again, I have changed the codeBera– Bera2018年07月31日 19:50:49 +00:00Commented Jul 31, 2018 at 19:50