CalculateField_management Expression Question

1248
4
Jump to solution
09-18-2013 10:11 AM
GeoffOlson
Occasional Contributor
I'm trying understand the CalculateField_management tool and I guess I just don't understand how to properly define a field to be a variable.  I'm just working in the Python Window for now, attempting to get my expression to work.  I'm just using a test shapefile until I understand how the function works, so I just have "FIELD1", "FIELD2", "FIELD3", AND "FIELD4" and when I run my expression I receive an error saying it can't concatenate 'str' and 'int' objects. Obviously this is a problem because all of my attribute fields are set up as number fields, 1 and 2 are short and 3 and 4 are double.  So do I need a line to define each column as a number since it seems to be defaulting to strings?  Here is my line:

arcpy.CalculateField_management("pythontesting", "FIELD3", ("FIELD1" + 1) * "FIELD2", "PYTHON")


That doesn't work, but if I replace my expression with 8 * 4, then I get the calculated value of 32.  What am I doing wrong?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ScottRutzmoser2
New Contributor III
Looks like a couple of things are causing you grief.
First the entire expression should be in single quotes.
arcpy.CalculateField_management("pythontesting", "FIELD3", ' ("FIELD1" + 1) * "FIELD2" ', "PYTHON")

Second you specify PYTHON so your syntax for the expression should be PYTHON..get rid of the quotes on the fields in the expression...like this:
'(!FIELD1! +1)* !FIELD2!'

The text below should work

arcpy.CalculateField_management("pythontesting", "FIELD3", '(!FIELD1! + 1) * !FIELD2!', "PYTHON")

If not I would recommend using the Field calculator in ArcMap to test your expression, then you can port it to the python script.

View solution in original post

0 Kudos
4 Replies
ScottRutzmoser2
New Contributor III
Looks like a couple of things are causing you grief.
First the entire expression should be in single quotes.
arcpy.CalculateField_management("pythontesting", "FIELD3", ' ("FIELD1" + 1) * "FIELD2" ', "PYTHON")

Second you specify PYTHON so your syntax for the expression should be PYTHON..get rid of the quotes on the fields in the expression...like this:
'(!FIELD1! +1)* !FIELD2!'

The text below should work

arcpy.CalculateField_management("pythontesting", "FIELD3", '(!FIELD1! + 1) * !FIELD2!', "PYTHON")

If not I would recommend using the Field calculator in ArcMap to test your expression, then you can port it to the python script.
0 Kudos
GeoffOlson
Occasional Contributor
You're right!!  In the field calculator that's how the field names get labeled...  It does work now, just I have to be in edit mode because it gives me a cannot acquire a lock error, can this be avoided?
0 Kudos
GeoffOlson
Occasional Contributor
I also found this from a thread a few years old, using an update cursor to be able to do the same thing but include an if statement so features get treated differently.  Thanks to Chris Snyder for his example snippet!

updateRows = arcpy.UpdateCursor("pythontesting", "", "", "FIELD3")
updateRow = updateRows.next()
while updateRow:
    if updateRow.FIELD1 == 1:
        updateRow.FIELD3 = (updateRow.FIELD1 + 1) * updateRow.FIELD2
    else:
        pass
    updateRows.updaterow(updateRow)
    updateRow = updateRows.next()
0 Kudos
ScottRutzmoser2
New Contributor III
If you are running it in ArcMap then you will likely need to be in an edit session...you can run it using the command line...try simply saving the file as somthing.py and then double clicking on it in windows explorer, a command prompt will open and run the script (make sure ArcMap is closed).  Or you can use a python ide like PyScripter (free) to run the script.  If you run it outside the ArcMap environment you shouldn't have an issue with a file lock, however, if you are running outside of ArcMap you will need to include the full path to the featureclass (first parameter).
Happy Scripting