I need to use CalculateField_management in a Python script, and the calc expression needs to concatenate a runtime variable, a string, and a field (string) value. I have searched all over for examples from which to learn. I have tried many things I have found in my research and some of my own ideas, too, but I just can't get the calc expression to evaluate. I am now completely confused and need some help to dig out of this hole. So, here's the scoop.
The field to be calculated, DESCRIP, is a string field in a fGDB feature class with one feature. The runtime variable, enddesg, will be a string. Essentially, what I want to do is concatenate the runtime value with a (longish) string and prepend that to the existing value in the DESCRIP field, storing the result back in the DESCRIP field. I have tried many forms of an "all-in-one" string for the expression, but nothing worked. I came to the conclusion that a pre-logic approach would be better, but I haven't been able to get that to work either. Below is what I have right now.
expr = "getDescription(str(enddesg))"
codeblock = """def getDescription(endstr):
exprstr = endstr + ' Touchdown Zone Elevation;duplicated from existing control point;' + !DESCRIP!
return exprstr"""
arcpy.CalculateField_management(perpptsSX_src, "DESCRIP", expr, 'PYTHON_9.3', codeblock) #Should be only one feature in the source
(BTW, the enddesg variable is either a string or an integer but is converted to a string as input.) I get the following error:
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000989: Python syntax error: Parsing error <type 'exceptions.SyntaxError'>: invalid syntax (line 2)
Failed to execute (CalculateField).
The following code variation produces no errors, but I also get nothing entered in the DESCRIP field.
expr = "getDescription(str(enddesg), !DESCRIP!)"
codeblock = """def getDescription(endstr, desc):
exprstr = endstr + ' Touchdown Zone Elevation;duplicated from existing control point;' + desc
return exprstr"""
I am now thoroughly confused.