In the following line, I don't understand what those exclamation marks mean? And why put everything in quotation? I don't understand the purpose of the variable expression, please explain to me as I'm new to python

expression = "crossing(!CROSS_ROW!,!ROW_SCORE!)"

codeblock = """def crossing(crosses, row_score):
            if crosses != 0:
                return 5
                return row_score"""

The code gives a score to pipes (from pipe layer) that cross, or passes next to roads (from road layer) depending on the width of the road. this was the code-block that assigned a score of 5 if the pipe crosses.

  • I am not familiar with Arcpy, but the ! must be specific to it or to Arcgis. It is not valid Python syntax. – Martijn Pieters Sep 17 '14 at 13:16
  • And because the exclamation marks are not Python code, I'd say that the Arcpy package post-processes these strings before executing the result as Python code. – Martijn Pieters Sep 17 '14 at 13:17
  • thats why i cant find anything in python forums, thanks at least i know where to look now :) – Solaire Sep 17 '14 at 13:18
  • 4
    Maybe this ArcGIS 9.3 Documentation page helps: Fields are always enclosed in exclamation points ! ! for Python. – Michael Jaros Sep 17 '14 at 13:24

What you are looking at is an Advanced Field Calculation.

It's a little confusing because you're kind of referencing it backwards. If you right-click on a field in an attribute table and select the field calculator, you'll notice an option in the field calculator window to change the parser to Python as well as a check box named 'Show Code Block'. When checked, the 'Show Code Block' check box allows you to perform an advanced field calculation by defining a custom function and using that function to calculate the values of the field.

Your Code Block would go into the Pre-Logic Script Code section as a custom function. Your Expression would go into the Field Calculation Expression section below that (In my case: Site_ID =)

enter image description here

So crossing() as defined in the Pre-Logic Script Code window is a function, which takes two parameters, crosses and row_score.

In the second window, the parameters (crosses and row_score) are being passed to the crossing() function by means of the values in the fields "CROSS_ROW" and "ROW_SCORE", respectively, for each record in the attribute table.

So in this function, the crossing() function will be executed against each value in the 'Site_ID' field. If the value held in the field "CROSS_ROW" for that particular record is not equal to 0, a value of 5 will be returned and populated into the 'Site_ID' field for that record. Otherwise, if the value held in the field "CROSS_ROW" is not equal to 0 (!=), then the value held in the field "ROW_SCORE" will be returned and populated into the 'Site_ID' field for that particular record.

EDIT: To more properly answer your question: encapsulating something in exclamation marks means value from the field named whatever is encapsulated. So the value of the field named !CROSSES! for this record. The reason everything is in quotes is because whoever wrote this was probably doing this through ESRI's ArcPy Site Package which would require this expressions and code blocks be passed in as a string.

| improve this answer | |
  • thank you very much, I kind of get it now, in fact I have used this very block in a bigger script. to change the final score in case of an intersection. – Solaire Sep 25 '14 at 19:19

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.