arcpy.management.CalculateField(CAD_Anno, "SymbolID", "!SymbolID!.replace('0', '1')", "PYTHON3")
Here is the syntax I wrote and the corresponding Error. I'm decent at python, but I am so confused about how to get this expression working. Basically, in the field of "SymbolID" I want to replace all the 0s with 1s. So the pseudocode would be "If there is a 0 in this current row of the field SymbolID, then replace it with a 1"
Traceback (most recent call last):
File "C:/Users/ggroshans/Desktop/append.py", line 14, in <module>
arcpy.management.CalculateField(CAD_Anno, "SymbolID", "!SymbolID!.replace('0', '1')", "PYTHON3")
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5194, in CalculateField
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5191, in CalculateField
retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type, enforce_domains), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000539: File "<expression>", line 1
0.replace('0', '1')
^
SyntaxError: invalid syntax
Failed to execute (CalculateField).
I just tested a similar code snippet in Pro 2.8.0, and everything worked as expected, i.e., no error and expected results. What desktop client and version are you using?
How are you executing the code? As a standalone script outside of Pro, a script tool in Pro, the interactive Python window in Pro, Jupyter notebook, etc...?
I tried several stand-alone scripts that basically do the same thing your code does, and all of them work as expected.
Given the error message, and the fact I can't reproduce it, make sure you are not copying pasting single and double quotes from other applications because it may be what you think is a single or double quote is not and the Python interpreter is reading your code differently than you expect.
The issue could be !SymbolID! is a numeric field.
Could you replace
"!SymbolID!.replace('0', '1')"
with
str(!SymbolID!).replace('0','1')
I tried converting it to string and got this error now:
Hi,
your field is type of Integer or string? if integer, you cannot search and replace strings.
instead of calculcate field, you can also (which is faster by the way) use arcpy.da.UpdateCursor
if your field is integer, you can simply (within cursor) check if your value is equal to 0 or not.