1

I am having a trouble in running the following scripts:

import arcpy
arcpy.env.workspace = r'C:\MyData\my_geodatabase.gdb'
max = 123
min = 0
mean_s = 12.3
expression1 = "test(!mydensity!)"
codeblock1 = """def test(value):
   if value <(%mean_s% + %min%)/2:     # if value <(12.3 + 0)/2:
      return "cat1"
   elif value < (%max% + %mean_s%)/2 and value >=(%min% + %mean_s%)/2:    #elif value < (123 + 12.3)/2 and value >=(12.3+ 0)/2:
      return "cat2"
   """
a ="a"
b = "b"
arcpy.CalculateField_management(a, b,expression1,"PYTHON", codeblock1)

this python script returns with error : arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid. ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 2) Failed to execute (CalculateField).

but the python works if I did the change in the code indicated after the (# ...)in the code above(in other words,if I change those %mean%,%max%,%min% to the real numbers, it works)

I guess it is because the codeblock in python wont recognize the max min mean_s defined at the beginning or something else goes wrong?

2 Answers 2

2

"my gdb" is nothing. it's a variable that you haven't declared. Instead, you want to put the path of your geodatabase in there; e.g.:

arcpy.env.workspace = r'C:\MyData\my_geodatabase.gdb'

You will run into other errors after that, but this is the current one.

You need to end the triple-quotes in your codeblock1. On the empty line above

a='a'

insert

"""

EDIT: Okay, with the new corrections to your code, we can get to the meat of it: your codeblock1 doesn't actually have the values; you're only putting in the variable names. Try this:

codeblock1 = """def test(value):
   if value <({0} + {1})/2:
      return "cat1"
   elif value < ({2} + {0})/2 and value >=({1} + {0})/2:
      return "cat2"
   """.format(mean_s, min, max)

Also, please do not assign min and max as variable names, since they're built-in Python variables. You won't be able to use the min and max functions anymore.

3
  • Thanks. I edited the code but it still fails to work. But if I change those %max% %mean% %min% to actual numbers , the script does work. Is there any way I could use the max mean and min instead of real numbers in the code block so that in future if I have different values to pass to max mean min they will be passed into code block ?
    – rayyht
    Commented Mar 19, 2016 at 17:11
  • @rayyht See my updated answer.
    – Tom
    Commented Mar 19, 2016 at 17:12
  • @rayyht Feel free to mark it as the answer, if it works.
    – Tom
    Commented Mar 20, 2016 at 0:14
2

I assume the paths, layer (a) and fields (b) are just representative. The code block of a field calculation written as a string has no access to variables defined in a python script. For your code to work you have to do value substitution in the string using the python format method. Your calculation also needs to output a value even when both of the conditions you wrote in your codeblock are false, so you need to include a final else clause. For example, if value = 70 then both original conditions fail (both 70 < 6.15 is False and (70 < 67.65 and 70 >= 6.15) is False).

import arcpy
arcpy.env.workspace = r'C:\MyData\my_geodatabase.gdb'
myMax = 123
myMin = 0
myMean_s = 12.3
expression1 = "test(!mydensity!)"
codeblock1 = """def test(value):
  if value < ({0} + {1})/2:     # if value <(12.3 + 0)/2:
    return 'cat1'
  elif value < ({2} + {0})/2 and value >=({1} + {0})/2:    #elif value < (123 + 12.3)/2 and value >=(12.3+ 0)/2:
    return 'cat2'
  else
    return None""".format(myMean_s, myMin, myMax)

a = "a"
b = "b"
arcpy.CalculateField_management(a, b, expression1,"PYTHON_9.3", codeblock1)
1
  • An error may occur if it Is ever possible for the mydensity field to contain Null values. If that is possible you should make the first condition of your codeblock1 check for Null and return None and then make the original first condition be an elif condition instead, i.e.: if value == None: \n return None \n elif value < ({0} + {1})/2: ... Commented Mar 19, 2016 at 21:56

Your Answer

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

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