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

1253
8
Jump to solution
02-15-2012 09:18 AM
MikeMacRae
Occasional Contributor III
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
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
I think you need to tell it your field variable is a field.
Something along these lines. And you should be using Python too.
arcpy.CalculateField_management(table, fieldnew, "!"+field+"!", "PYTHON", "")

View solution in original post

0 Kudos
8 Replies
MathewCoyle
Frequent Contributor
I think you need to tell it your field variable is a field.
Something along these lines. And you should be using Python too.
arcpy.CalculateField_management(table, fieldnew, "!"+field+"!", "PYTHON", "")
0 Kudos
markdenil
Occasional Contributor III
Furthermore, I don't think CalculateField accepts a field object; it is expecting the name string between the proper database specific field name decorations.
the VB version would be:
"[" + field.name + "]"

to wit:
arcpy.CalculateField_management(table, fieldnew, "[" + field.name + "]", "VB", "").
0 Kudos
MikeMacRae
Occasional Contributor III
Hey thanks Matt. You're always pretty helpful. I tried your work around using python, but it errored out again. I took your advice and designated the field.name as a field in VB and it works great. Script below:

arcpy.CalculateField_management(table, fieldnew, "["+field.name+"]", "VB", "")


Edit: Thanks for responding as well Mark. I think I caught that just before you responded, but thanks for helping out. That's exactly what I needed to do.

Mike
0 Kudos
ChrisSnyder
Regular Contributor III
This:

arcpy.CalculateField_management(table, fieldnew, "["+field.name+"]", "VB", "")

is equavalent to this:

arcpy.CalculateField_management(table, fieldnew, "!"+field.name+"!", "PYTHON", "")

unless 'table' is actually a feature layer or table view that contains a join. In that case, you can only use the "VB" syntax with the square brackets.
0 Kudos
BrittanyGale
Occasional Contributor
Hello,

I am trying to calculate a new field based on a pre-existing field as well. I applied some of the above tips to my script, but nothing seems to be working. Can one of you experts please help me debug my code?

>>> arcpy.CalculateField_management(inTable, "PARCEL_APN", "["+map_apn+"]", "VB", "")
Runtime error <type 'exceptions.NameError'>: name 'map_apn' is not defined


The 'map_apn' is a legitimate field.

Thank you in advance! I really appreciate all the help I can get, especially since this is my first time writting my own script.

Brittany
0 Kudos
BrittanyGale
Occasional Contributor
Also, sorry for the bother. I don't mean to butt into the conversation. I just figure that this is the best way to for me to get the attention of the people who know what they're talking aboutm, and that posting my own forum would be a little redundant.
0 Kudos
markdenil
Occasional Contributor III
If, as I think you mean, 'map_apn' is the actual field name, and not a variable holding the field name,
(a resonable assumption given both the error messege and your statement: 'The 'map_apn' is a legitimate field.')
then you don't want to concatinate the parts of the string.

to wit, use:
"[map_apn]"
0 Kudos
BrittanyGale
Occasional Contributor
Thank you so much! That worked. I don't know what I would do without the help of the people on these forums. I look forward to helping others once I gain some more knowledge and experience. Have a wonderful day!
0 Kudos