3

I'm beginner in python and learning it with examples. I have 100s of dbf tables like

    Type value1 value2           Type value1 value2
       A      3      1    and       A      5      7 
       B      4      2              A      6      8

I want them to simply merged in single file like

    Type value1 value2 
       A      3      1
       B      4      2
       A      5      7
       B      6      8

to get this from arcpy I used the code as below but it doesn't return any results or any errors. I have made "alldata.dbf" at that place with exact column to work with but the updateCursor doesn't update anything on that file. Can anyone help? or is it blunder to do thing like this in python instead of other easier method?

import arcpy

arcpy.env.workspace = r'D:/dbflst/'
dbfList = arcpy.ListTables()

finalTable = r'D:/alldata.dbf'

for dbf in dbfList:
dbfFile = arcpy.env.workspace + '/' + dbf
dbfData = {}

# read in data from DBF and create dictionary
with arcpy.da.SearchCursor(dbfFile, ['Type','Value1','Value2']) as cursor:
    for row in cursor:
        dbfData[row[0]] = row[1]
del cursor

# write data from dictionary to final table
with arcpy.da.UpdateCursor(finalTable, ['Type','Value1','Value2']) as cursor:
    for row in cursor:
        row[1] = dbfData[row[0]]
        cursor.updateRow(row)
del cursor
3

The Merge tool would be easier.

However, if you want to persist with python, some comments below:

Your indentation is incorrect, should be something like:

for dbf in dbfList:
    dbfFile = arcpy.env.workspace + '/' + dbf
    dbfData = {}

    # read in data from DBF and create dictionary
    with arcpy.da.SearchCursor(dbfFile, ['Type','Value1','Value2']) as cursor:
        etc...

You should use an InsertCursor, not an UpdateCursor to add new rows:

with arcpy.da.InsertCursor(finalTable, ['Type','Value1','Value2']) as cursor:
    etc...

Also consider putting the InsertCursor outside the for dbf in dbfList loop, so you only create it once, something like:

#Assumes "finalTable" already exists!
with arcpy.da.InsertCursor(finalTable, ['Type','Value1','Value2']) as ins_cursor:
    for dbf in dbfList:
        dbfFile = arcpy.env.workspace + '/' + dbf

        # read in data from DBF and create dictionary
        with arcpy.da.SearchCursor(dbfFile, ['Type','Value1','Value2']) as src_cursor:
            for row in src_cursor:
                ins_cursor.insertRow(row)
  • Thank you! As I am learning python I preferred to do simple (which I thought) things in it but required whole lot of research including a question in stackexchange :) – Nammy Oct 9 '15 at 2:32
2

To do this, instead of an update cursor, you need to use an insert cursor (arcpy.da.InsertCursor):

InsertCursor establishes a write cursor on a feature class or table. InsertCursor can be used to add new rows.

An update cursor modifies existing rows, and you probably have an empty table, so you need to insert rows into it.

Instead of a cursor, you may also want to consider using the Append (or Merge) tool:

Appends multiple input datasets into an existing target dataset. Input datasets can be point, line, or polygon feature classes, tables, rasters, raster catalogs, annotation feature classes, or dimensions feature classes.

To combine input datasets into a new output dataset, use the Merge tool.

  • (same answer because you both are right and great) Thank you! As I am learning python I preferred to do simple (which I thought) things in it but required whole lot of research including a question in stackexchange :) – Nammy Oct 9 '15 at 2:37

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.