Using wildcards with Cursors

596
2
07-25-2011 02:06 PM
JasonTaylor
New Contributor
Can anyone help me place a wildcard where I have an asterisk for '*a' and '*b'? I know this code isn't right, almost got it though.

import arcpy

PATCH_MASTER = "C:\\PATCHES\\PATCH_MASTER.shp"

rows = arcpy.UpdateCursor(PATCH_MASTER)

for row in rows:
    if row.PATCH_CODE == '*a' or row.PATCH_CODE == '*b':
        if row.FID_PATCH_ <= 8:
            row.PATCH_CODE = date_code + "00" + str(row.FID_PATCH_ + 1)
            rows.updateRow(row)
        elif row.FID_PATCH_ <= 98 and row.FID_PATCH_ >= 8:
            row.PATCH_CODE = date_code + "0" + str(row.FID_PATCH_ + 1)
            rows.updateRow(row)
        else:
            row.PATCH_CODE = date_code + str(row.FID_PATCH_ + 1)
            rows.updateRow(row)

del row
del rows


Thanks!
Tags (2)
0 Kudos
2 Replies
Luke_Pinner
MVP Regular Contributor
Simple answer - you can't use wildcards for python string comparisons, use something like:
#assuming you are checking the LAST character for 'a' or 'b'...
if row.PATCH_CODE[-1] in ['a','b']:


Complicated answer - you can use wildcards for python string comparisons if you use regular expressions - http://docs.python.org/library/re.html
0 Kudos
JasonTaylor
New Contributor
Very cool, thanks for the response. I had a feeling it wasn't straight forward. The work around makes total sense. Thanks again!
0 Kudos