Select to view content in your preferred language

Update Cursor using wildcards

2069
5
10-03-2012 06:04 AM
JasonMoan
New Contributor
I am new to Python and I'm having trouble writing a script to process some forestry data.

I have a feature class called "Survey" and in this feature class is a text field called CODE. The data in CODE are comma delimited, but contain varying amounts of text and are not necessarily in a specific order, for example: "M,C,1," or "Hw,D,Con,L,3". In this example C and Hw are host types, C = Conifer and Hw = Hardwood. What I'm trying to do is this:
1. iterate through the rows looking for "*C,*" anywhere within the CODE field and if found
2. update another field, HOST, with "Conifer"

Any help is greatly appreciated.
Tags (2)
0 Kudos
5 Replies
by Anonymous User
Not applicable
Perhaps something like this would work:

surveyFc = 'Survey'
query = '"CODE" LIKE \'*C*\''

urows = arcpy.UpdateCursor(surveyFc, query)
for row in urows:
    row.setValue("HOST", "Conifer")
    urows.updateRow(row)
del row, urows
0 Kudos
SebastianSantibanez
Emerging Contributor
Hi Jason,

You need to take a good look at this pages for the selection by wildcard
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s500000033000000

Untested, but probably your selection query will look something like
'"thefield" LIKE'+"'"+thewildcard+"'"

and for updating your values to your new field read this
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000
and this
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Calculate_Field_examples/00170000004s0...

I hope it helps
0 Kudos
ChrisSnyder
Honored Contributor
Not sure how all that hokey looking "\" syntax keeps getting propogated (oh yeah, model builder and the Help pages), but it is generally completely unneccessary.

The wildcards of *, %, or ? depend of course on the database (and GIS data format) you are using...

This "should" work:

#Select anything with a 'C,' string patern in it (Assuming FGDB/SDE format) and make the HOST field = 'Conifer'
urows = arcpy.UpdateCursor(surveyFc, "CODE LIKE '%C,%')
for row in urows:
    row.setValue("HOST", "Confier") #Note row.HOST = "Confier" also works
    urows.updateRow(row)
del row, urows
0 Kudos
JasonMoan
New Contributor
Great! This did work!

My question now is how would I set this up in an if statement? I think I'd need to change the urows variable first and then edit the For loop, but I can't seem to get the syntax right. What I'm trying to do is populate the HOST field with whatever HOST code is present in the CODE field. HOST is just one of many fields that I will be populating from the information in this CODE field.

For example:
if CODE contains '%C,%' enter 'Conifer' in HOST
else if CODE contains '%Hw,%' enter 'Hardwood' in HOST
if CODE contains neither, HOST should be Null

Thanks!

Not sure how all that hokey looking "\" syntax keeps getting propogated (oh yeah, model builder and the Help pages), but it is generally completely unneccessary.

The wildcards of *, %, or ? depend of course on the database (and GIS data format) you are using...

This "should" work:

#Select anything with a 'C,' string patern in it (Assuming FGDB/SDE format) and make the HOST field = 'Conifer'
urows = arcpy.UpdateCursor(surveyFc, "CODE LIKE '%C,%')
for row in urows:
    row.setValue("HOST", "Confier") #Note row.HOST = "Confier" also works
    urows.updateRow(row)
del row, urows
0 Kudos
ChrisSnyder
Honored Contributor
Doesn't use wildcards exactly, but how about something simple like:

urows = arcpy.UpdateCursor(surveyFc)
for row in urows:
    if 'C,' in updateRow.CODE
        row.setValue("HOST", "Confier")
    elif 'Hw,' in updateRow.CODE
        row.setValue("HOST", "Hardwood")
    else:
        row.setNull("HOST")
    urows.updateRow(row)
del row, urows


If you want to use "wildcards" in a cursor, I think you would want to use a regular expression-based method: http://code.google.com/edu/languages/google-python-class/regular-expressions.html
0 Kudos