AnsweredAssumed Answered

Create New Fields and Calculate New Field Based on Pre-existing Field

Question asked by BrokenLegMike on Feb 15, 2012
Latest reply on Feb 21, 2012 by bgale88
Hey folks,

I am trying to set a python script to:


  1. List fields in a table

  2. Add a new field based on the current field by adding a "_1" to the end (i.e. if I have a number of fields named a1, b1, c1, etc. I want to add some new fields called a1_1, b1_1, c1_1, etc)

  3. Field calculate the new field based on it's original field (i.e. calculate a1_1 from the values in a1, calculate b1_1 with the values from b1 and so on)

  4. Delete the original field

The fields are being added perfectly, but when I go to calculate the new fields based on the old, it keeps erroring out with no real traceback message:

PYTHON ERRORS:
Traceback info:
  File "Z:\ESRI\Python\Test Scripts\SpeciesTable.py", line 25, in <module>
    arcpy.CalculateField_management(table, fieldnew, field, "VB", "")

Error Info:
Object: Error in executing tool

ArcPy ERRORS:


I have a feeling it doesn't like the variable for the new field, but I've played with a  number of different approaches and it doesn't seem to work. Also, I thought it was because the field types are different (i.e. all the original fields are TEXT or DOUBLE. The new fields are all SHORT). I tested this by manually adding the fields and field calculating from the attribute table and that seems to work fine.  My script is as follows:

import arcpy, sys, traceback from arcpy import env  env.overwriteOutput = True  env.workspace = r"C:\data\temp.gdb"  tableList = arcpy.ListTables()  for table in tableList:     #print table     if table == "TestTable":         fieldList = arcpy.ListFields(table)                      for field in fieldList:                  if field.name not in ["OBJECTID", "TYPE", "Common_Name", "Scientific_Name", "Species"]:                     #print field.name                     fieldnew = field.name + "_1"                     #print fieldnew                     arcpy.AddField_management(table, fieldnew, "SHORT")                     arcpy.CalculateField_management(table, fieldnew, field, "VB", "")                     arcpy.DeleteField_management(field)          del field, fieldList, fieldnew 


Thanks,
Mike

Outcomes