3

I have a table with a column named "Kategorie", filled with values. I added a new column named "RULE". I want to fill this column with Strings depending on "KATEGORIE". I have a PY-Script and it doesn't work. What is wrong?

Expression:

"getKat(!KATEGORIE!)"

Codeblock:

"""def getKat(KATEGORIE):
    if KATEGORIE = "0":
        myval = "Autobahn"
    if KATEGORIE = "1":
        myval = "Bundesstrasse"
    else:
        myval= 3"""
arcpy.CalculateField_management(ERG, "RULE", expression, "", codeblock)

Thank you for your answers !

But both don't work. I think the problem is the line "expression" or "codeblock". I try to print the result of "myval".The result is a error message "name myval is not defined"

expression:

"getKat(!KATEGORIE!)"

codeblock:

"""def getKat(KATEGORIE):
    if KATEGORIE == "0":
        myval == "Autobahn"
    elif KATEGORIE == "1":
        myval == "Bundesstrasse"
    else:
        myval == "3"
    return myval"""
print "myval", myval
arcpy.CalculateField_management(ERG, "RULE", expression, "PYTHON", codeblock)
  • 1
    Where are you executing your code block? In the python window in arcmap, as a standalone script, or in the Calculate field dialog? – blord-castillo Oct 10 '13 at 15:11
  • in your expression have you changed it to getKat(!RULE!) since this is the field you are trying to calculate? – Craig Oct 14 '13 at 15:42
5

You need to change = to == in your if statements in your code block. I'd also recommend using elif as well:

"""def getKat(KATEGORIE):
    if KATEGORIE == "0":
        myval = "Autobahn"
    elif KATEGORIE == "1":
        myval = "Bundesstrasse"
    else:
        myval= 3
    return myval"""

In python (and most languages) = is used for assignment and == is used for comparison.

As Craig mentioned, you'll also need to return myval at the end.

4

Read up on Calculator Field Examples.

I would recommend using elif instead of multiple if statements and also using return instead of myval = ""

example:

if KATEGORIE == "0":
    return "Autobahn"
elif KATEGORIE == "1":
    return "Bundesstrasse"
else:
    return "other"
2

I think this may be what you are after - the syntax is not one that I try to remember.

I simply ran the Calculate Field tool once interactively using the Expression and Code Block from your question (with some minor fixes) and then did Copy As Python Snippet in the Geoprocessing | Results window to get:

# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "layerName"
arcpy.CalculateField_management("layerName","RULE","getKat( !KATEGORIE! )","PYTHON_9.3","""def getKat(KATEGORIE):/n  if KATEGORIE == "0":/n    myVal = "Autobahn"/n  elif KATEGORIE == "1":/n    myVal = "Bundesstrasse"/n  else:/n    myVal = "Other"/n  return myVal""")

From there, as @Paul pointed out (and I had not tested), you also need to change "/n" to "\n" in all occurrences to get working code.

arcpy.CalculateField_management("fishnetPolys","KATEGORIE","getKat( !KATEGORIE! )","PYTHON_9.3","""def getKat(KATEGORIE):\n  if KATEGORIE == "0":\n    myVal = "Autobahn"\n  elif KATEGORIE == "1":\n    myVal = "Bundesstrasse"\n  else:\n    myVal = "Other"\n  return myVal""")
  • 1
    One thing you have to remember is to change /n to \n. I don't know why it exports this way. +1 as this way is really quick and you know what you are getting. – Paul Oct 12 '13 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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