2

I am trying to find the correct syntax for the UpdateCursor where I look for part of a text from another field. I have many values of PCD zoning codes (PCD 03, PCD 04, PCD 05, etc). I want to calculate my "ZONING_DESC" field based off these PCD values in my "ZONING" field where all PCD values get calculated to "Planned Commercial Development". I know that '==' operator is finding exact values. Is there an operator for finding like values?

Here is my Code:

with arcpy.da.UpdateCursor(Zoning, ["ZONING", "ZONING_DESC"]) as cursor:
    if row[0] == 'PCD%':
        row[1] = 'Planned Commercial Development'
    cursor.updateRow(row)
3

In place of the SQL wildcard %, use the Python string method startswith as below.

with arcpy.da.UpdateCursor(Zoning, ["ZONING", "ZONING_DESC"]) as cursor:
    for row in cursor:
        if row[0].startswith('PCD'):
            row[1] = 'Planned Commercial Development'
            cursor.updateRow(row)
  • Thanks @BERA, didn't notice that wasn't in the code snippet. Edited answer to reflect this. – smiller May 3 '18 at 17:12
1

If you are simply trying to test if the ZONING field starts with PCD, then you can just write a python expression to test if the 3 left most characters equal PCD. That would look something like:

if row[0][:3] == 'PCD':

You may want to check out documentation on String Slices (such as at https://developers.google.com/edu/python/strings)

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.