1

I'm trying to copy an attribute value from one field to another (sum of raster values), but I'm having issues in writing a functioning expression for a Calculate Field script. I'm adding a field called SUM and trying to copy the value from the field "SUM_" + filename[0:-4]. I have multiple files I am trying to do this on and the field names are different in each, which is why it is being written in this manner.

Here's the code I have so far:

for table in arcpy.ListTables():
    arcpy.AddField_management(table, "SUM", "DOUBLE")
    expression = ["SUM_" + str(filename[0:-4])]
    arcpy.CalculateField_management(table, "SUM", expression, "VB")  

This doesn't work as I receive the following error:

File "C:\Users\kellyj\Desktop\Projects\CostAnalysis\Route_Cost_Analysis.py", line 82, in <module>
    arcpy.CalculateField_management(table, "SUM", expressionvalue, "VB")
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 3354, in CalculateField
    raise e
RuntimeError: Object: Error in executing tool

Any thoughts as to how I can fix this?

  • Thanks @Aaron. The script now finishes without an error, but the new "SUM" field is 0 in all of the shapefiles. Am I pointing to the source field properly in the expression? – user28939 Sep 8 '14 at 20:40
  • Your script is creating the SUM field as a numeric Double field and then trying to calculate a string into it. Numeric fields cannot have non-numeric strings calculated into them. The SUM field has to be a string with enough characters to hold the concatenated "SUM_" plus the longest base file name. – Richard Fairhurst Sep 8 '14 at 20:56
  • The reason Aaron's code produced 0 is that the input was without any delimiters and was interpreted as a variable. An uninitialized variable would be interpreted as 0. – Richard Fairhurst Sep 8 '14 at 21:08
  • Use expression = "[SUM_" + str(filename[0:-4]) + "]" to make a field name inside a string. When interpreted by the Field Calculator the string from Python is just a field name, not a string. – Richard Fairhurst Sep 8 '14 at 21:11
3

You have to have a square bracket surrounding the "SUM_" thing

Trying change

expression = ["SUM_" + str(filename[0:-4])]

to

expression = "[SUM_{0}]".format(str(filename[0:-4]))
2

The brackets have to be inside the string, since otherwise the brackets will create a Python list. When Python passes a string that has brackets inside of it as the expression, Python does not interpret the brackets, but the Field Calculator interprets the bracketed expression within the string as a field name. Use the code that follows:

for table in arcpy.ListTables():
    arcpy.AddField_management(table, "SUM", "DOUBLE")
    expression = "[SUM_{0}]".format(str(filename[0:-4]))
    arcpy.CalculateField_management(table, "SUM", expression, "VB")
  • you are right, I tought that he wanted a string, not the field value. – radouxju Sep 9 '14 at 7:11

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.