2

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\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"
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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 25, 2014 at 15:58
0

1 Answer 1

6

Try adjusting your code, your /n should be \n:

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")

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)
answered Aug 25, 2014 at 16:13
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.