Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

Any assistance in pointing out where the syntax error is occurring would be appreciated.

UPDATE:

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.

share|improve this question
    
Have you considered dropping the Calculate Field operation and switching to arcpy.da.UpdateCursor()? –  Aaron Aug 25 at 16:04
3  
Suggestion: create a variable which will contain the function you are trying to apply. Otherwise it is extremely hard to troubleshoot when everything is in one row. This variable will be a string with triple quotes ("""def... """) and all the Python syntax (indentations etc) should be in place. Post it like this in the question body. –  Alex Tereshenkov Aug 25 at 16:06
    
@Aaron, not yet - I am really wanting to determine what is causing this issue. In the event it becomes necessary, I would look into alternative approaches (such as using an UpdateCursor). –  evv_gis Aug 25 at 16:07
    
@AlexTereshenkov, I will give that a shot and post back the results. –  evv_gis Aug 25 at 16:07
    
@AlexTereshenkov, change was made and in doing so, the script worked. If you'd like to make it an answer, I will accept. –  evv_gis Aug 25 at 16:21

1 Answer 1

up vote 5 down vote accepted

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)
share|improve this answer
    
thank you for the post. Your approach is very similar to Alex's in that it uses the triple quote around the code. Since Alex did in fact reply first, I am allowing him to make his comment an answer. However, have upvoted your answer because it does also include the UpdateCursor approach. –  evv_gis Aug 25 at 16:23
1  
@evv_gis, thanks for the feedback. I suggest accepting the Jason's answer who was not lazy like me to republish the code and find the error ;) cheers. –  Alex Tereshenkov Aug 25 at 16:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.