AnsweredAssumed Answered

Update Table with values from Second Table

Question asked by BrokenLegMike on Feb 16, 2012
Latest reply on Feb 22, 2012 by BrokenLegMike
Hey everyone,

I am trying to update a table with values from a second table. Basically, I have a table with field types that are all wrong (they have DOUBLES and TEXT types that are not what my client wants) and I want to fix that using python. Here are my steps:


  1. Create new table using arcpy.CreateTable_management

  2. Get a count of the rows from the original table and use arcpy.insertcursor on the second table to insert the same amount of rows.

  3. List the fields from the original table and add them as new fields to the new table using arcpy.AddField

  4. Set a search cursor to read through the original table and use arcpy.updateCursor to update these values into the second table

Here is where I am hung up. The first 4 fields will be static. They will always be in every 'original' table I use. The next fields will be dynamic. The amount of fields and the field names may change from one 'original' table to the next. My script can handle creating the new table and adding the fields dynamically, but I'm kind of hung up on the update cursor part. I want the search cursor/update cursor to be able to read through the fields dynamically and set the values to each appropriate field from the original to the new. I'm hoping this is the right approach. Please see the last code block of my script. I've put a couple comments in there. I keep getting errors on:

update.getValue(names) = row.getValue(names)


import arcpy, sys, traceback from arcpy import env  env.overwriteOutput = True  env.workspace = r"Z:\Test.gdb"  arcpy.CreateTable_management(r"Z:\Test.gdb", "TABLE_E2_1_new") count =  int(arcpy.GetCount_management("TABLE_E2_1_original").getOutput(0))  rows = arcpy.InsertCursor("TABLE_E2_1_new") x = 1  while x <= count:     print x     row = rows.newRow()     rows.insertRow(row)     x+=1 del row, rows, x  tableList = arcpy.ListTables()  for table in tableList:     if table == "TABLE_E2_1_original":         fieldList = arcpy.ListFields(table)                      for field in fieldList:             if field.name not in ["OBJECTID", "TYPE", "Common_Name", "Scientific_Name", "Species"]:                 print field.name                 arcpy.AddField_management("TABLE_E2_1_new", field.name, "SHORT")             elif field.name != "OBJECTID":                 print "Remaining " + field.name                 arcpy.AddField_management("TABLE_E2_1_new", field.name, "TEXT", "", "", 100)  del table, tableList, fieldList, field  searchRows = arcpy.SearchCursor("TABLE_E2_1_original") updateRows = arcpy.UpdateCursor("TABLE_E2_1_new")  update = updateRows.next()  fieldList = arcpy.ListFields("TABLE_E2_1_new")  for row in searchRows:             # List fields to get field name and pass into variable for calling into update cursor/search cursor below             for field in fieldList:                  names = field.name                   update.getValue(names) = row.getValue(names) # Keep getting error "***can't assign to function call.                  update = updateRows.next()  del update, updateRows, row, searchRows, names, field, fieldList

Outcomes