Using Python for Calculate Field

397
3
08-07-2012 12:03 PM
KristenE
New Contributor III
I am new to Python and was hoping someone could help me with this code. I just want to do a simple calculate field (as part of a larger script) and calculate one field to a few characters of text. All the examples I found online were much more complicated than what I needed help with. The following code results this error message:

ExecuteError: ERROR 999999: Error executing function.
Expected end of statement
Failed to execute (CalculateField).

I get the error message for the first Calculate Field.

Here is the code:
[INDENT]
import arcpy
from arcpy import env

# Define variables

env.workspace = r"Z:\package\CPP_scratch.gdb"

# This file name will change each month
eoptreps_v2 = r"Z:\package\Polygon_package.gdb\eoptreps_20120806"

#####

print "Calculating project field - 1307"
arcpy.CalculateField_management(eoptreps_v2, "Project", "1307 - PNDI CPP")

print "Calculating project field - 1350"

if arcpy.Exists("eoptreps_lyr"):
    arcpy.Delete_management("eoptreps_lyr")
arcpy.MakeFeatureLayer_management(eoptreps_v2, "eoptreps_lyr")

if arcpy.Exists("StateForest_lyr"):
    arcpy.Delete_management("StateForest_lyr")
arcpy.MakeFeatureLayer_management(r"Z:\StateLayers.gdb\Boundaries_Natural\PA_StateForest", "StateForest_lyr")

arcpy.SelectLayerByLocation_management("eoptreps_lyr", "INTERSECT", "StateForest_lyr")

arcpy.CalculateField_management("eoptreps_lyr", "Project", "1350 - DCNR")
[/INDENT]


Things I have tried:

  • Changing "1307 - PNDI CPP" (the expression) to '[Project] = "1307 - PNDI CPP"' This results in the script running as if it works, but then when I check the field it is still NULL.

  • Changing the expression to "PGLMT PNDI" in case the dash was causing a problem. Same error message here.

Tags (2)
0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Please read this.

http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code

As to your problem. What are your field names and types?
0 Kudos
curtvprice
MVP Esteemed Contributor
You need string delimeters inside the expression, which is itself a string. In Python this is easily done by escaping the double quote, or using the other kind of python string delimeter outside:
arcpy.CalculateField_management(eoptreps_v2, "Project", "\"1307 - PNDI CPP\"")
arcpy.CalculateField_management(eoptreps_v2, "Project", '"1307 - PNDI CPP"')


Double-quotes are used here because although we are using python, the default syntax expression for the Calculate Field tool is VBScript, which delimits string literals with double quotes.
0 Kudos
KimOllivier
Occasional Contributor III
If you are in a Python script already I recommend that you do not use CalculateField, use a cursor.
CalculateField is useful in ModelBuilder and interactive table manipulation but not in a script.
It is no faster than a cursor (especially at 10.1 da module)  because CalculateField simply wraps a cursor around the expression.

With a cursor you can:

  • Trap errors with a try/except block

  • Test for unexpected data and handle it properly (eg null values)

  • Debug easily by adding print statements

  • Write the expressions without complicated syntax (no !wrappers! or [access_speclal] brackets)

  • Do multiple field calculations at once

  • Use a dictionary to update values to avoid a join