I'm having a similar problem with my if statement in the field calculator. I've joined two tables for point features and want to copy the street address (Street_Number) from the joined table to the existing field (StreetNumber) only if the existing field is blank.
Here's what I have so far:
#Copy Street Number to Curbstop from Parcel if Streetnumber is blank
#This code does not include the spatial join this is based off of, verify
#that CurbStop_SpatialJoin1 is available
#Import modules
import arcpy
from arcpy import env
#Set environment
#Location for gisintern pc local copy of NR Municipal DB
#Change to use elsewhere
env.workspace = "C:\Users\gisintern\Documents\NewRichmondMunicipal.mdb"
#Set variables for field calculations
inTable = "Water\CurbStop"
fieldname = "StreetNumber"
expression = "copystreetno(str(!CurbStop_SpatialJoin1.Street_Number!))"
codeblock = """def copystreetno(CurbStop_SpatialJoin1.Street_Number):
parcelstnum = CurbStop_SpatialJoin1.Street_Number
CSstnum = CurbStop.StreetNumber
if CSstnum = "":
CSstnum = parcelstnum
return CSstnum """
#Calculate Field
arcpy.CalculateField_management(inTable, fieldname, expression, "PYTHON", codeblock)
This returns this error in the Python window in ArcMap 10
"Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 1)"
and this error if run in the python shell
"Traceback (most recent call last):
File "C:/Users/gisintern/Documents/parcel_st_num_to_CS", line 30, in <module>
arcpy.CalculateField_management(inTable, fieldname, expression, "PYTHON", codeblock)
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 2727, in CalculateField
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 1)
Failed to execute (CalculateField)."
Any ideas where I'm going wrong?