<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Update existing rows/add new rows in master table from records in second table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682753#M52900</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Regarding the use of&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt; testFields = arcpy.ListFields(masterTable)&lt;/PRE&gt;&lt;SPAN&gt; instead of &lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;testFields = ['APN_SUFFIX', 'TRA', 'NAME_1', 'NAME_2', 'MAIL_ADDR', 'CTY_STA']
&lt;/PRE&gt;&lt;SPAN&gt;: when I use the former I get the dread ERROR 999999 at the line &lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;subsetDict[row.getValue(keyField)] = [row.getValue(field) for field in testFields]
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What am I doing wrong?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;P.S. I ran the script on the entire 262,000 + table for the six fields shown above, and it took 13 minutes in the IDLE GUI. The old method, a korn shell script interacting with SDE, took about 10, for all 100 + fields. (I first tried running it in the ArcCatalog Python window, and it is still running more than 2 hours later. YIKES!)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 04:43:53 GMT</pubDate>
    <dc:creator>MarkHuffman</dc:creator>
    <dc:date>2021-12-12T04:43:53Z</dc:date>
    <item>
      <title>Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682732#M52879</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a table that is a subset of a master table representing the parcel database. This subset is created every week and contains only updated and new records, and my task is to compare that table to the master table and, if existing records have different values, update those fields, and if there are new records, add those records to the master table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This process has up to now been done in SDE using "sdeimport -o update_else_insert." I want to avoid interacting with SDE, but will if necessary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; My plan is to use SearchCursor to read the subset and the master table, and UpdateCursor and InsertCursor to edit the master table, using the APN as the key field. First, is this the right approach? Second, can someone please provide some starting code or psuedocode to help me? I am new at Python but not at programming. Thank you.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Feb 2012 17:54:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682732#M52879</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2012-02-13T17:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682733#M52880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Mark,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;there are a huge number of ways you could do this, I would suggest taking advantage of Python dictionaries if you can... Maybe something like so (should be almost ready to run, except for any errors/typo's):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy

# paths to tables
masterTable = '//masterTable'
subsetTable = '//subsetTable'

# define fields:
keyField = 'APN'
testFeild1 = 'a'
testField2 = 'b'
#... and so on
## If you know all your fields and will never need to change them I would recommend using the 'dynamic' noation row.APN inseatd of row.getValue('APN') as I have used below

# dictionary of the contents of the subset table
# APN is key, fields to test are stroed in an ordered list
subsetDict = {}
sC = arcpy.SearchCursor(subsetTable)
for row in sC:
 subsetDict[row.getValue(keyField)] = [row.getValue(testField1), row.getValue(testField2)]
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(testField1), row.getValue(testField2)]

 # if records have changed add to updateDuct and delete from subsetDict
 if subsetDict[key] != testList:
&amp;nbsp; updateDict[key] = subsetDict[key]
&amp;nbsp; del subsetDict[key]
 # records are the same, delete from subsetDict
 else:
&amp;nbsp; del subsetDict[key]
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)
 row.setValue(testField1, subsetDict[key][0])
 row.setValue(testField2, subsetDict[key][1])
 iC.insertRow(row)
 del row
del iC, subsetDict

# anything in updateDuct needs to be updated, use updateCursor
uC = arcpy.UpdateCursor(masterTable)
for row in uC:
 if row.getValue(keyField) in updateDict:
&amp;nbsp; row.setValue(testField1, updateDict[key][0])
&amp;nbsp; row.setValue(testField2, updateDict[key][1])
del uC, row, updateDict&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The code takes a key field and two test fields (fields tested to see if they have been updated), but would be pretty expandable to any number of test fields...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:43:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682733#M52880</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-12T04:43:35Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682734#M52881</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;H Stacy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm near speechless with gratitude. I will study this code, adapt it, and give it a try. Thanks so much.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Feb 2012 19:46:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682734#M52881</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2012-02-13T19:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682735#M52882</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Stacy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am running the code with my test tables and fields, and I am encountering "Runtime error &amp;lt;type 'exceptions.KeyError'&amp;gt;: u'018021107'". I have narrowed it down to occurring in or after this block:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;if subsetDict[key] != testList:
&amp;nbsp; updateDict[key] = subsetDict[key]
&amp;nbsp; del subsetDict[key]
 # records are the same, delete from subsetDict
 else:
&amp;nbsp; del subsetDict[key]
del sC, row&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And here is my code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;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'
testField1 = 'APN_SUFFIX'
testField2 = 'TRA'
testField3 = 'NAME_1'
testField4 = 'NAME_2'
testField5 = 'MAIL_ADDR'
testField6 = 'CTY_STA'

#... and so on
## If you know all your fields and will never need to change them I would recommend using the 'dynamic' notation row.APN instead of row.getValue('APN') as I have used below

# 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(testField1), row.getValue(testField2), row.getValue(testField3), row.getValue(testField4), row.getValue(testField5), row.getValue(testField6)]
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(testField1), row.getValue(testField2), row.getValue(testField3), row.getValue(testField4), row.getValue(testField5), row.getValue(testField6)]

 # if records have changed add to updateDict and delete from subsetDict
 if subsetDict[key] != testList:
&amp;nbsp; updateDict[key] = subsetDict[key]
&amp;nbsp; del subsetDict[key]
 # records are the same, delete from subsetDict
 else:
&amp;nbsp; del subsetDict[key]
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)
 row.setValue(testField1, subsetDict[key][0])
 row.setValue(testField2, subsetDict[key][1])
 row.setValue(testField3, subsetDict[key][2])
 row.setValue(testField4, subsetDict[key][3])
 row.setValue(testField5, subsetDict[key][4])
 row.setValue(testField6, subsetDict[key][5])
 iC.insertRow(row)
 del row
del iC, subsetDict

# anything in updateDict needs to be updated, use updateCursor
uC = arcpy.UpdateCursor(masterTable)
for row in uC:
 if row.getValue(keyField) in updateDict:
&amp;nbsp; row.setValue(testField1, updateDict[key][0])
&amp;nbsp; row.setValue(testField2, updateDict[key][1])
&amp;nbsp; row.setValue(testField3, updateDict[key][2])
&amp;nbsp; row.setValue(testField4, updateDict[key][3])
&amp;nbsp; row.setValue(testField5, updateDict[key][4])
&amp;nbsp; row.setValue(testField6, updateDict[key][5])&amp;nbsp; 
del uC, row, updateDict&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you please take another look? Thank you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:43:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682735#M52882</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2021-12-12T04:43:38Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682736#M52883</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Mark,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sorry - I wrote it but wasn't able to test it, so I didn't pick this up. It is occurring where there are records in the master table that don't exist in the subset table (that is fine, and from your description, expected), this causes a &lt;/SPAN&gt;&lt;STRONG&gt;KeyError&lt;/STRONG&gt;&lt;SPAN&gt; from the dictionary (accessing a key that doesn't exist). So I have added a try...except statement that tells it to &lt;/SPAN&gt;&lt;STRONG style="font-style: italic;"&gt;pass&lt;/STRONG&gt;&lt;SPAN&gt; (do nothing) if a key error occurs for a particular APN...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; Just replace the second Search Cursor block with this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# 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(testField1), row.getValue(testField2), row.getValue(testField3), row.getValue(testField4), row.getValue(testField5), row.getValue(testField6)]

 # 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:
&amp;nbsp; # if records have changed add to updateDict and delete from subsetDict
&amp;nbsp; if subsetDict[key] != testList:
&amp;nbsp;&amp;nbsp; updateDict[key] = subsetDict[key]
&amp;nbsp;&amp;nbsp; del subsetDict[key]
&amp;nbsp; # records are the same, delete from subsetDict
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp; del subsetDict[key]
 except KeyError:
&amp;nbsp; # this APN only exists in the master table, do nothing...
&amp;nbsp; pass
del sC, row&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hopefully that should work fine now!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;p.s. How does it compare speed-wise to the method you were using previously? Well, you would probably only notice a difference if the tables are pretty large, but there are a few things you could do to speed it up a little...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:43:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682736#M52883</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-12T04:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682737#M52884</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Stacy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Great! Thanks. Making progress. The master table is now being updated with new rows from the subset table. Strangely the first new row is being placed last in the master table, and a KeyError is being called on its key field value and, although a print command placed after the last line confirms that the script is completing, the insertCursor block does not seem to be working.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Feb 2012 23:37:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682737#M52884</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2012-02-13T23:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682738#M52885</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Mark,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The master table is now being updated with new rows from the subset table&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; Strangely the first new row is being placed last in the master table&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you mean: 1. 'the new rows are being added at the end', or 2. ' the top-most new row is bottom-most in the updated table'? Either way:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. The InsertCursor adds a new row to the bottom of the table, so this is expected.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Dictionary keys are not actually ordered; so the order in which the rows are added to the table is (effectively) random.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; and a KeyError is being called on its key field value&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I need more detail on this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A KeyError means that it is asking the dictionary for a key that doesn't exist. Which block is this occurring in?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;and, although a print command placed after the last line confirms that the script is completing, the insertCursor block does not seem to be working.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If the Insert Cursor is not working, new rows would not be being added from the subset table (your first comment) - do you mean the Update Cursor?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If there are still issues, can you attach your test files and I will try to sort them out...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Feb 2012 00:48:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682738#M52885</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-02-14T00:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682739#M52886</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Stacy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wrote my last post in a rush while doing something else. I'll clarify.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Do you mean: 1. 'the new rows are being added at the end', or 2. ' the top-most new row is bottom-most in the updated table'? Either way:&lt;BR /&gt;1. The InsertCursor adds a new row to the bottom of the table, so this is expected.&lt;BR /&gt;2. Dictionary keys are not actually ordered; so the order in which the rows are added to the table is (effectively) random.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I meant #2 - but that's okay since they are added in random order, as you say.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I need more detail on this.&lt;BR /&gt;&lt;BR /&gt;A KeyError means that it is asking the dictionary for a key that doesn't exist. Which block is this occurring in?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The KeyError calls out the APN value of the last new record in the master table: Runtime error &amp;lt;type 'exceptions.KeyError'&amp;gt;: u'203006146'&amp;nbsp; -- That is the last record in the newly updated master table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;If the Insert Cursor is not working, new rows would not be being added from the subset table (your first comment) - do you mean the Update Cursor?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes, I meant the Update Cursor. The error is occurring in the Update Cursor block. The changes to the existing records in the subset table are not updated to the master table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again for all your time and help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Feb 2012 15:00:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682739#M52886</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2012-02-14T15:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682740#M52887</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Mark,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;can you send me the files you are testing with (you can attach files to posts)? If you have to, zip them together.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I just can't figure what the problem might be without being able to test stuff and see it for myself.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Feb 2012 18:02:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682740#M52887</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-02-14T18:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682741#M52888</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Mark,&lt;BR /&gt;&lt;BR /&gt;can you send me the files you are testing with (you can attach files to posts)? If you have to, zip them together.&lt;BR /&gt;&lt;BR /&gt;I just can't figure what the problem might be without being able to test stuff and see it for myself.&lt;BR /&gt;&lt;BR /&gt;Stacy&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have sent you a private message to arrange to send the data by more secure means. Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Feb 2012 20:46:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682741#M52888</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2012-02-14T20:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682742#M52889</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Stacy,&lt;BR /&gt;&lt;BR /&gt;I have sent you a private message to arrange to send the data by more secure means. Thanks.&lt;BR /&gt;&lt;BR /&gt;Mark&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Mark, I received your message but for some reason I cannot reply or send you a new Private Message (seems to send, but nothing appears in the the Sent Items folder, and you obviously haven't received anything).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Feb 2012 23:30:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682742#M52889</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-02-14T23:30:53Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682743#M52890</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hate to barge in on a good conversation, but it looks like you are missing an update in your update cursor.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for row in uC:
 if row.getValue(keyField) in updateDict:
&amp;nbsp; row.setValue(testField1, updateDict[key][0])
&amp;nbsp; row.setValue(testField2, updateDict[key][1])
&amp;nbsp; row.setValue(testField3, updateDict[key][2])
&amp;nbsp; row.setValue(testField4, updateDict[key][3])
&amp;nbsp; row.setValue(testField5, updateDict[key][4])
&amp;nbsp; row.setValue(testField6, updateDict[key][5])
&amp;nbsp; &lt;STRONG&gt;uC.updateRow(row)&lt;/STRONG&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:43:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682743#M52890</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-12T04:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682744#M52891</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hate to barge in on a good conversation, but it looks like you are missing an update in your update cursor.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
for row in uC:
 if row.getValue(keyField) in updateDict:
&amp;nbsp; row.setValue(testField1, updateDict[key][0])
&amp;nbsp; row.setValue(testField2, updateDict[key][1])
&amp;nbsp; row.setValue(testField3, updateDict[key][2])
&amp;nbsp; row.setValue(testField4, updateDict[key][3])
&amp;nbsp; row.setValue(testField5, updateDict[key][4])
&amp;nbsp; row.setValue(testField6, updateDict[key][5])
&amp;nbsp; &lt;STRONG&gt;uC.updateRow(row)&lt;/STRONG&gt;
&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi Mathew,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Feel free to barge in any time. Thanks for the catch.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm still getting the error, which is occurring before uC.UpdateRow.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:43:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682744#M52891</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2021-12-12T04:43:46Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682745#M52892</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I was executing code similar to this.&amp;nbsp; Have you tried declaring the 'key' in the update cursor.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for row in uC:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;key = row.getValue(keyField)&lt;/STRONG&gt;
 if row.getValue(keyField) in updateDict:
&amp;nbsp; row.setValue(testField1, updateDict[key][0])
&amp;nbsp; row.setValue(testField2, updateDict[key][1])
&amp;nbsp; row.setValue(testField3, updateDict[key][2])
&amp;nbsp; row.setValue(testField4, updateDict[key][3])
&amp;nbsp; row.setValue(testField5, updateDict[key][4])
&amp;nbsp; row.setValue(testField6, updateDict[key][5])
&amp;nbsp; uC.updateRow(row)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:43:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682745#M52892</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-12T04:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682746#M52893</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I was executing code similar to this.&amp;nbsp; Have you tried declaring the 'key' in the update cursor.&amp;nbsp; Ex:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for row in uC:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;key = row.getValue(keyField)&lt;/STRONG&gt;
 if row.getValue(keyField) in updateDict:
&amp;nbsp; row.setValue(testField1, updateDict[key][0])
&amp;nbsp; row.setValue(testField2, updateDict[key][1])
&amp;nbsp; row.setValue(testField3, updateDict[key][2])
&amp;nbsp; row.setValue(testField4, updateDict[key][3])
&amp;nbsp; row.setValue(testField5, updateDict[key][4])
&amp;nbsp; row.setValue(testField6, updateDict[key][5])
&amp;nbsp; uC.updateRow(row)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Hi Jake,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That was the final piece. My script is working now! Actually &lt;/SPAN&gt;&lt;STRONG&gt;our&lt;/STRONG&gt;&lt;SPAN&gt; script. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you, Jake, Mathew, and especially Stacy.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:43:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682746#M52893</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2021-12-12T04:43:51Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682747#M52894</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Mark,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;that's great news! Writing is the easy part - it's fixing up all the little mistakes that usually takes the majority of the time, as you have seen!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How does that script compare with your former method; speed, reliability, etc.?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Feb 2012 01:51:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682747#M52894</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-02-16T01:51:34Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682748#M52895</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Also, if anyone is interested, here is the code to perform this operation for arbitrary tables (fields only defined once at the start):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy from arcpy import env&amp;nbsp; # Define workspace env.workspace = "C:/PythonParcelProcesses"&amp;nbsp; # paths to tables masterTable = 'UpdateInsertTo.dbf' subsetTable = 'UpdateInsertFrom.dbf'&amp;nbsp; # define fields: keyField = 'APN' testFields = ['APN_SUFFIX', 'TRA', 'NAME_1', 'NAME_2', 'MAIL_ADDR', 'CTY_STA']&amp;nbsp; # 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:&amp;nbsp; subsetDict[row.getValue(keyField)] = [row.getValue(field) for field in testFields] del sC, row&amp;nbsp; # get list of values that need to be updated updateDict = {} sC = arcpy.SearchCursor(masterTable) for row in sC:&amp;nbsp; key = row.getValue(keyField)&amp;nbsp; testList = [row.getValue(field) for field in testFields]&amp;nbsp;&amp;nbsp; # try to get record in subsetDict to see if it has changed&amp;nbsp; # if the subset table didn't contain a record for this entry, will trow KeyError (that's fine - do nothing)&amp;nbsp; try: &amp;nbsp; # if records have changed add to updateDict and delete from subsetDict &amp;nbsp; if subsetDict[key] != testList: &amp;nbsp;&amp;nbsp; updateDict[key] = subsetDict[key] &amp;nbsp;&amp;nbsp; del subsetDict[key] &amp;nbsp; # records are the same, delete from subsetDict &amp;nbsp; else: &amp;nbsp;&amp;nbsp; del subsetDict[key]&amp;nbsp; except KeyError: &amp;nbsp; # this APN only exists in the master table, do nothing... &amp;nbsp; pass del sC, row&amp;nbsp; # anything left in subsetDict is new, can use insertCursor iC = arcpy.InsertCursor(masterTable) for key in subsetDict:&amp;nbsp; row = iC.newRow()&amp;nbsp; row.setValue(keyField, key)&amp;nbsp; for i in range(len(testFields)): &amp;nbsp; field = testFields&lt;I&gt; &amp;nbsp; row.setValue(field, subsetDict[key]&lt;I&gt;)&amp;nbsp; iC.insertRow(row)&amp;nbsp; del row del iC&amp;nbsp; # anything in updateDict needs to be updated, use updateCursor uC = arcpy.UpdateCursor(masterTable) for row in uC:&amp;nbsp; key = row.getValue(keyField)&amp;nbsp; if row.getValue(keyField) in updateDict: &amp;nbsp; for i in range(len(testFields)): &amp;nbsp;&amp;nbsp; field = testFields&lt;I&gt; &amp;nbsp;&amp;nbsp; row.setValue(field, updateDict[key]&lt;I&gt;) &amp;nbsp; uC.updateRow(row) del uC, row, updateDict, subsetDict&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Feb 2012 01:57:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682748#M52895</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-02-16T01:57:16Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682749#M52896</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Also, if anyone is interested, here is the code to perform this operation for arbitrary tables (fields only defined once at the start):&lt;BR /&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, Stacy. This is what I was looking for - a way to define the fields once at the beginning and be done with it. I've got over a hundred total.:D&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How does that script compare with your former method; speed, reliability, etc.?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I expect it will run faster and be more reliable, as well as be easier to edit and update.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Feb 2012 21:15:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682749#M52896</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2012-02-16T21:15:40Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682750#M52897</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Mark,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Stacy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Feb 2012 21:31:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682750#M52897</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2012-02-16T21:31:08Z</dc:date>
    </item>
    <item>
      <title>Re: Update existing rows/add new rows in master table from records in second table</title>
      <link>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682751#M52898</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi Mark,&lt;BR /&gt;&lt;BR /&gt;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...&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Stacy&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I thought there had to be a way to access an existing list: the table itself. Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I really need to get more proficient at this. Do you recommend any books? I have "Learning Python," by Mark Lutz (O'Reilly).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Mark&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Feb 2012 21:36:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-existing-rows-add-new-rows-in-master-table/m-p/682751#M52898</guid>
      <dc:creator>MarkHuffman</dc:creator>
      <dc:date>2012-02-16T21:36:37Z</dc:date>
    </item>
  </channel>
</rss>

