Automation tip for Python-based field calculations

By Charlie Frye, Esri Chief Cartographer

I do most of my data automation in Python–I’m no expert with Python, but I learned the basics a few years ago and that’s served me well. One of the tasks I frequently use Python for is automating the management of attributes, i.e., adding, deleting, and calculating fields.  I learned VB prior to Python, so I had always used VBScript as my basis for field calculations.  Recently, I learned that doing field calculations in Python scripts was not as stable as using Python code as the basis for my calculate statements.  The issue, for me, was having my code not run, generating a bogus generic error.  The development team figured it out and they’re working on fixing it for the 9.4 release.

In the meantime, it became obvious that I needed to learn how to use Python in my field calculations and there was nothing I could find in the online help to guide me in terms of proper syntax. I got some help from our geoprocessing team and here are three examples that might help you:

  • Calculate one field’s value from another field.  The trick here was to put exclamation points around the field name (versus brackets, single or double quotes, or any escape characters).

gp.CalculateField(OutSimpConts, ElevFld,”!CONTOUR!”,”PYTHON”,”#”)

  • Calculate a field value from a Python variable. After the above example, this one was deceptively easy. In this case, “tile” is an integer variable that I set in my Python script.  No special characters needed, just make sure your Python variable is the right type (str(), etc. may be useful functions for converting variables).

gp.CalculateField_management(OutSimpConts,”Tile_ID”,tile,”PYTHON”,”#”)

  • Last, here’s one that uses an advanced calculate statement.  This is the statement I got help on (can you see why?!).  Basically, I am calculating the Idx20_YN field based on ElevFld Mod 20.  In Python the Mod function is accomplished with the % character.  You can also see that the Python indentation scheme is also captured here:

gp.CalculateField_management(OutSimpConts,”Idx20_YN”, “k(!” + ElevFld + “!)”, “PYTHON”, “def k(elev):rn    if elev % 20 == 0:rn        return 0rn    else:rn        return 1″)

This entry was posted in Mapping and tagged . Bookmark the permalink.

Leave a Reply

3 Comments

  1. kimo says:

    Using CalculateField tool in a Python script is possible but very difficult to debug and impossible to handle data exceptions.

    I recommend that if you are writing a script, then use a cursor and do the equivalent calculation within the loop. It’s no slower because the CalculateField tool just wraps a cursor around the expression anyway.

    If you have a null value, then you can easily get an error if doing arithmetic. It is easy to test values are within range and handle them differently inside a cursor.

    if you have a syntax error, then Python will tell you where is is and provide a better hint on the problem. I have found that my scripts work much better now.

  2. clj2289 says:

    That’s a great point–in fact I used to test my VBScript calculate expressions first using VBA with dummy variables. That way I could use the VBA environment to help me write the code correctly the first time.

  3. cfrye says:

    That’s a great point–in fact I used to test my VBScript calculate expressions first using VBA with dummy variables. That way I could use the VBA environment to help me write the code correctly the first time.