Dan_Patterson

Create a table using python

Blog Post created by Dan_Patterson Champion on Jul 27, 2014

This example shows how to create a dbf file, in a folder, and add fields and some sample data to it.  See the example setup after the line...

if __name__ == "__main__":

section of the script.  This script could easily be modified to accept command line parameters for the output filename.

 

'''
CreateTableDemo.py
  Author:
    Dan Patterson
    Dept of Geography and Environmental Studies
    Carleton University, Ottawa, Canada
    Dan.Patterson@carleton.ca
  Date created:  March 16, 2011
  Modified:      July 2013, Aug 2014
  Purpose:
    To demonstrate how to create tables using a simple function
  Requires:
    The user provides a table name and path, a list of fields and a list of data
    See the example setup after the line...    if __name__ == "__main__":
'''
def createTableFile (output_tbl, data_list, fieldsToAdd, arcpy):
  '''
    Requires:
    output_tbl  output feature class name "c:/temp/myfile.dbf"
    data_list   a list of lists of data eg.
                [[data list 1],[data list 2]...[data list n]]
            or  [[data list 1]]
    arcpy     the Geoprocessor Object
    Returns:
    Create the output table and returns a message (msg)
  '''
  fullName = os.path.split(output_tbl)
  outFolder = fullName[0].replace("\\","/")
  outFName =  fullName[1].replace(" ", "_")
  outFullName = outFolder + "/" + outFName
  msg = "\n"
  try:
    arcpy.CreateTable_management(outFolder, outFName)
    msg +=  "\nCreating:   " + str(outFName)
  except:
    msg += "\nFailed to create feature class:  " + arcpy.GetMessages()
    return msg
  for aField in fieldsToAdd:
    msg += "\nAdding field:  " + str(aField)
    try:
      arcpy.AddField_management(outFullName, aField[0], aField[1], aField[2], aField[3])
    except:
      msg += "\nFailed to add field: " + aField[0] + " " +arcpy.GetMessages()
  #
  #Create the insert cursor
  try:
    cur = arcpy.InsertCursor(output_tbl)
  except:
    msg += "Failed to create cursor"
    return msg
  anID=0
  for i in range(0,len(data_list)):
    values = data_list[i]
    a_row = cur.newRow()
    for j in range(0, len(values)):
      a_row.setValue(fieldsToAdd[j][0],values[j])
    cur.insertRow(a_row)
  del a_row
  del cur
  msg += "\nProcessing complete"
  return msg
#----------------------------------------------------------
if __name__ == "__main__":
  import sys, os
  import arcpy
  arcpy.env.overwriteOutput = True
  outDBF = "C:/temp/testdbf.dbf"      #!!!!!! change to suit or make into a parameter
  if os.path.exists(outDBF):
    os.remove(outDBF)
  #Sample data and fields to add to the table
  data_list = [[1,1,1,"a"],[2,2,2,"b"],[3,3,3,"c"],[4,4,4,"d"],[5,5,5,"e"]]
  fieldsToAdd = [["X", "double", 14, 7],
                 ["Y", "double", 14, 7],
                 ["Z", "double", 14, 7],
                 ["TextFld", "text", 20,""]]
  msg = createTableFile(outDBF, data_list, fieldsToAdd, arcpy)
  print msg

Outcomes