Solved! Go to Solution.
Stacy,
I have sent you a private message to arrange to send the data by more secure means. Thanks.
Mark
for row in uC: if row.getValue(keyField) in updateDict: row.setValue(testField1, updateDict[key][0]) row.setValue(testField2, updateDict[key][1]) row.setValue(testField3, updateDict[key][2]) row.setValue(testField4, updateDict[key][3]) row.setValue(testField5, updateDict[key][4]) row.setValue(testField6, updateDict[key][5]) uC.updateRow(row)
Hate to barge in on a good conversation, but it looks like you are missing an update in your update cursor.for row in uC: if row.getValue(keyField) in updateDict: row.setValue(testField1, updateDict[key][0]) row.setValue(testField2, updateDict[key][1]) row.setValue(testField3, updateDict[key][2]) row.setValue(testField4, updateDict[key][3]) row.setValue(testField5, updateDict[key][4]) row.setValue(testField6, updateDict[key][5]) uC.updateRow(row)
for row in uC: key = row.getValue(keyField) if row.getValue(keyField) in updateDict: row.setValue(testField1, updateDict[key][0]) row.setValue(testField2, updateDict[key][1]) row.setValue(testField3, updateDict[key][2]) row.setValue(testField4, updateDict[key][3]) row.setValue(testField5, updateDict[key][4]) row.setValue(testField6, updateDict[key][5]) uC.updateRow(row)
I was executing code similar to this. Have you tried declaring the 'key' in the update cursor. Ex:for row in uC: key = row.getValue(keyField) if row.getValue(keyField) in updateDict: row.setValue(testField1, updateDict[key][0]) row.setValue(testField2, updateDict[key][1]) row.setValue(testField3, updateDict[key][2]) row.setValue(testField4, updateDict[key][3]) row.setValue(testField5, updateDict[key][4]) row.setValue(testField6, updateDict[key][5]) uC.updateRow(row)
import arcpy from arcpy import env # Define workspace env.workspace = "C:/PythonParcelProcesses" # paths to tables masterTable = 'UpdateInsertTo.dbf' subsetTable = 'UpdateInsertFrom.dbf' # define fields: keyField = 'APN' testFields = ['APN_SUFFIX', 'TRA', 'NAME_1', 'NAME_2', 'MAIL_ADDR', 'CTY_STA'] # dictionary of the contents of the subset table # APN is key, fields to test are stored in an ordered list subsetDict = {} sC = arcpy.SearchCursor(subsetTable) for row in sC: subsetDict[row.getValue(keyField)] = [row.getValue(field) for field in testFields] del sC, row # get list of values that need to be updated updateDict = {} sC = arcpy.SearchCursor(masterTable) for row in sC: key = row.getValue(keyField) testList = [row.getValue(field) for field in testFields] # try to get record in subsetDict to see if it has changed # if the subset table didn't contain a record for this entry, will trow KeyError (that's fine - do nothing) try: # if records have changed add to updateDict and delete from subsetDict if subsetDict[key] != testList: updateDict[key] = subsetDict[key] del subsetDict[key] # records are the same, delete from subsetDict else: del subsetDict[key] except KeyError: # this APN only exists in the master table, do nothing... pass del sC, row # anything left in subsetDict is new, can use insertCursor iC = arcpy.InsertCursor(masterTable) for key in subsetDict: row = iC.newRow() row.setValue(keyField, key) for i in range(len(testFields)): field = testFields row.setValue(field, subsetDict[key]) iC.insertRow(row) del row del iC # anything in updateDict needs to be updated, use updateCursor uC = arcpy.UpdateCursor(masterTable) for row in uC: key = row.getValue(keyField) if row.getValue(keyField) in updateDict: for i in range(len(testFields)): field = testFields row.setValue(field, updateDict[key]) uC.updateRow(row) del uC, row, updateDict, subsetDict
Also, if anyone is interested, here is the code to perform this operation for arbitrary tables (fields only defined once at the start):
How does that script compare with your former method; speed, reliability, etc.?
Hi Mark,
It is not even necessary for you to list all the fields yourself, arcpy.ListFields(table) should do the trick. The purpose of what I had was (started off doing it, then forgot to carry it through) that you could have certain fields that were tested for matching while the other fields were just copied if there was a difference in the test fields, and of course for new entries...
Regards,
Stacy