Using Wildcard to extract values with Field Calculator?

3480
2
02-07-2013 09:51 AM
DanEckberg
New Contributor
I'm trying to extract a portion of an existing field in an attribute table to a new field using Field Calculator and have been running into some difficulties. It seems like there must be a way to do this, although with my limited VBS and Python experience I can't be certain. I'm working with 10.0 (sp. 2)

The exact problem is this:

The layer I'm working with has a very long field, [PopUp] (which was created when converting a KMZ file to a feature class) whose contents for each feature look something like this:


...<td>LAND USE CODE</td> <td>954</td>...


I want to extract just the Land Use Code from the [PopUp] field and populate a new field with it (either a numeric field or string, I don't care).

The Land Use Code changes based on the feature in question and the length of the string varies for each feature both before and after this segment which I want to extract (so I can't use a function which just grabs X number of characters from the left or right side).

Is this possible using the Field Calculator, with either Python or VBScript?

If so, I assume it would require some way to indicate a wildcard on either side of the Land Use Code, but I know it doesn't work the same as a simple SQL query/search.

Any help would be greatly appreciated.

Thanks in advance!
Tags (2)
0 Kudos
2 Replies
by Anonymous User
Not applicable
Are you familiar with cursors?  This can be done very easily with an update cursor:

fc = r'path\to\your\featureClass'
rows = arcpy.UpdateCursor(fc)
for row in rows:
    row.Land_Use = int(''.join([i for i in row.PopUp if i.isdigit()]))
    rows.updateRow(row)


Where 'Land_Use' is the new field you want to extract the values to (I had it set up as a short integer field, hence the int() value).  The fc variable is just the path pointing to your feature class.  This just runs a simple test to extract all values within the text string that are integers.
0 Kudos
DanEckberg
New Contributor
Thanks, that worked and helped lead me to my ultimate solution, which was as follows (and conducted from the stand-alone Python Window, not Field Calculator):

import re
fc='file path'
rows=arcpy.UpdateCursor(fc)
for row in rows:
     row.FieldName = re.search('LAND USE CODE</td>\n<td>(\d*)<' , row.PopUp).group(1)
     rows.updateRow(row)
0 Kudos