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)

Both answers so far 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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 10, 2013 at 14:53
2
  • 1
    Where are you executing your code block? In the python window in arcmap, as a standalone script, or in the Calculate field dialog? Commented Oct 10, 2013 at 15:11
  • in your expression have you changed it to getKat(!RULE!) since this is the field you are trying to calculate? Commented Oct 14, 2013 at 15:42

3 Answers 3

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.

answered Oct 10, 2013 at 15:20
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"
answered Oct 10, 2013 at 15:23
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""")
answered Oct 12, 2013 at 6:30
1
  • 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. Commented Oct 12, 2013 at 19:07

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.