3

I am trying to append ~10,000 tables together using an ArcPy script.

I believe the method I am using does not work as the script is attempting to append too many tables at the same time. Would a possible work-around be a script that appends using looping?

import os
import sys
import string
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")

WD = "C:/workspace"
arcpy.env.workspace = WD

#selects the 10,000 tables and tries to append them all together at the same time

arcpy.CreateTable_management(out_path=WD, out_name="append", template="C:/Workspace/template", config_keyword="")
tables1 = [table for table in arcpy.ListTables() if not table.startswith('append')]
arcpy.Append_management(inputs=tables1, target="ndvi_append", schema_type="TEST", field_mapping="", subtype="")
  • 6
    You may be better off just looping through all the tables and using an search cursor to read values and an insert cursor to add the rows. – crmackey Sep 21 '16 at 15:51
3

Here's a code sample to implement crmackey's suggestion above. 1)Open an insert cursor for your final location, 2) loop each table, 3) check if table doesn't start with append, 4) read table data with a search cursor, consumed in a list, 5) use insert cursor to push table data into final table.

insertCursor = arcpy.da.InsertCursor("ndvi_append", "*")
for table in arcpy.ListTables():
    if not table.startswith('append'):
        tableData = list(arcpy.da.SearchCursor(table, "*"))
        for data in tableData:
            insertCursor.insertRow(data)
  • 1
    The location of InsertCursor line seems incorrect! – fatih_dur Sep 22 '16 at 0:22
  • 1
    Thanks @fatih_dur for your feedback. I updated my answer so that the insert cursor is only created once instead of every cycle of the loop. – Priscilla Sep 22 '16 at 15:15
  • thank you! code ran perfectly. also worked when the cursor was created every cycle of the loop – gis_user Sep 22 '16 at 22:24
  • 1
    @user6665295, cursors are relatively expensive in terms of computational resources that you have. Obviously it is inserting a new row to the table whether you create once at the outset or numerous times with each loop, so practically working. But the current answer is logically correct. Also I suggest using with statement with arcpy.da cursors for a better efficacy. – fatih_dur Sep 23 '16 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.