I'm transitioning python code from an Add-in used in ArcMap to ArcGIS Pro. I decided to use a Python notebook for one of the scripts. I am copying the code from the Add-In and pasting it into the notebook.
The line below is causing an error.
arcpy.CalculateField_management(CountyAgSUM, "GISdeed", "a(!FIRST_FIRS!)", "PYTHON_9.3", "def a(FIRST_FIRS):\\n if FIRST_FIRS == \"AC\":\\n return \'0\'\\n else:\\n return FIRST_FIRS")
And the error it's throwing is listed here.
--------------------------------------------------------------------------- ExecuteError Traceback (most recent call last) In [44]: Line 83: arcpy.CalculateField_management(CountyAgSUM, "GISdeed", "a(!FIRST_FIRS!)", "PYTHON_9.3", "def a(FIRST_FIRS):\\n if FIRST_FIRS == \"AC\":\\n return \'0\'\\n else:\\n return FIRST_FIRS") File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField: Line 5209: raise e File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in CalculateField: Line 5206: retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type), True))) File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>: Line 511: return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: Failed to execute. Parameters are not valid. ERROR 000989: Python syntax error: File "<string>", line 1 def a(FIRST_FIRS):\n if FIRST_FIRS == "AC":\n return '0'\n else:\n return FIRST_FIRS ^ SyntaxError: unexpected character after line continuation character Failed to execute (CalculateField).
Any suggestions on what I need to do to fix it are appreciated.
try replacing the double quotes within the code block with single quotes.
a = "def a(FIRST_FIRS):\\n if FIRST_FIRS == \'AC\':\\n return \'0\'\\n else:\\n return FIRST_FIRS"
a
"def a(FIRST_FIRS):\\n if FIRST_FIRS == 'AC':\\n return '0'\\n else:\\n return FIRST_FIRS"