Having trouble creating an expression for CalculateField()

587
8
06-22-2021 10:46 AM
GeoDev
by
New Contributor II
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).

 

0 Kudos
8 Replies
JoshuaBixby
MVP Esteemed Contributor

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?

0 Kudos
GeoDev
by
New Contributor II
I am also on ArcGIS Pro 2.8.0. Not sure how yours worked and mine is
getting this error message.
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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...?

0 Kudos
GeoDev
by
New Contributor II
I'm executing it via standalone script in PyCharm. I have PyCharm connected
to the Python 3.7 (arcgispro-py3) environment.
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

The issue could be !SymbolID! is a numeric field.

Could you replace 

"!SymbolID!.replace('0', '1')"

with

str(!SymbolID!).replace('0','1')

 



Think Location
0 Kudos
GeoDev
by
New Contributor II

I tried converting it to string and got this error now: 

GeoDev_0-1624386897578.png

 

0 Kudos
Tomasz_Tarchalski
New Contributor III

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.

 

0 Kudos