Using Python for Calculate Field

Discussion created by erath on Aug 7, 2012
Latest reply on Aug 8, 2012 by kimo
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:
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.MakeFeatureLayer_management(eoptreps_v2, "eoptreps_lyr")

if arcpy.Exists("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")

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.