More

Python Syntax Error 000989 when running Python Snippet of successful geoprocess?

Python Syntax Error 000989 when running Python Snippet of successful geoprocess?


I am having an issue with a simple python snippet.

Here's the background: I wanted to include an additional step in a nightly update script that replaces characters in a field that simply do not play nice with a web application that allows users to query the data. These characters included angle brackets ("<" and ">") which I was trying to replace with "Less Than" and "Greater Than".

So when I was testing the Field Calculation, I finally got it to work the way I wanted to, and as I have done with so many other snippets, I simply copied the Python Snippet of the geoprocess, and pasted into my script.

Below is the snippet:

arcpy.CalculateField_management("path/to/my/layer","field","RemoveBad( !field!)","PYTHON_9.3","def RemoveBad(x):/n x = x.replace("<=", " Less Than or Equal To " )/n x = x.replace(">=", " Greater Than or Equal To ")/n x = x.replace("<", " Less Than ")/n x = x.replace(">", " Greater Than ")/n x = x.replace("=", " Equal To ")/n x = x.strip()/n return x")

Now in my script, I do have a log that reports geoprocessing errors, so when I run the script, I am getting the following:

Executing: CalculateField path	omylayer field RemoveBad( !field!) PYTHON_9.3 "def RemoveBad(x):/n x = x.replace('<=', ' Less Than or Equal To ' )/n x = x.replace('>=', ' Greater Than or Equal To ')/n x = x.replace('<', ' Less Than ')/n x = x.replace('>', ' Greater Than ')/n x = x.replace('=', ' Equal To ')/n x = x.strip()/n return x" Start Time: Mon Aug 25 09:35:32 2014 Failed to execute. Parameters are not valid. ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1) Failed to execute (CalculateField). Failed at Mon Aug 25 09:35:32 2014 (Elapsed Time: 0.00 seconds)

So I started combing through the script but I cannot find the issue, which leads me to believe it has something to do with the angle brackets.

To reiterate, this python snippet is from a successful Field Calculator geoprocess.


Per Alex's feedback I created the following variable:

RemoveBad = """def RemoveBad(x): x = x.replace("<=", " Less Than or Equal To " ) x = x.replace(">=", " Greater Than or Equal To ") x = x.replace("<", " Less Than ") x = x.replace(">", " Greater Than ") x = x.replace("=", " Equal To ") x = x.strip() return x"""

Then I updated the snippet:

arcpy.CalculateField_management("path/to/my/layer","field","RemoveBad( !field!)","PYTHON_9.3",RemoveBad)

The error is now gone and script is working as expected.


Try adjusting your code, your/nshould be :

arcpy.CalculateField_management("path/to/my/layer","field","RemoveBad( !field!)","PYTHON_9.3","def RemoveBad(x):
 x = x.replace("<=", " Less Than or Equal To " )
 x = x.replace(">=", " Greater Than or Equal To ")
 x = x.replace("<", " Less Than ")
 x = x.replace(">", " Greater Than ")
 x = x.replace("=", " Equal To ")
 x = x.strip()
 return x")

Or better yet, put your code block in a triple-quoted string so you can read it (and you don't need to escape your quotes):

arcpy.CalculateField_management("path/to/my/layer", "field", "RemoveBad(!field!)", "PYTHON_9.3", """def RemoveBad(x): x = x.replace("<=", " Less Than or Equal To " ) x = x.replace(">=", " Greater Than or Equal To ") x = x.replace("<", " Less Than ") x = x.replace(">", " Greater Than ") x = x.replace("=", " Equal To ") x = x.strip() return x""")

and if you'd rather do it as a cursor:

def fixvalue(val): return (val.replace("<=", " Less Than or Equal To " ) .replace(">=", " Greater Than or Equal To ") .replace("<", " Less Than ") .replace(">", " Greater Than ") .replace("=", " Equal To ") .strip()) with arcpy.da.UpdateCursor('path/to/my/layer', "field") as cur: for row in cur: row[0] = fixvalue(row[0]) cur.updateRow(row)

Watch the video: Python SyntaxError invalid syntax