Search Cursor - Search Entire Row?

829
6
09-06-2011 01:38 PM
BenYoungs
New Contributor II
All,

This may be an obvious question but I am having a little trouble with this... I am writing a script to loop through all the feature classes in a given workspace, and I need to search all fields (or all string fields, at minimum) in each feature class based on a provided string.

Essentially, I need to do what the 'Find' command in ArcMap is supposed to do, except I need to run this automatically on hundreds or potentially thousands of feature classes.

I have been playing around with a Search Cursor to do this, but I havent been able to iterate through each field for every row of the table successfully.

I am sure I could call the 'Select by Attributes' tool in Python and re-run that tool for each field in the feature class, but that seems inefficient to me.

Has anyone done anything like this before? Am I over-thinking this one and there is a simple solution?

I am using 9.3.1 in this instance but could write it for 10 if necessary. Thanks in advance.
Tags (2)
0 Kudos
6 Replies
DarrenWiens2
MVP Honored Contributor
Inside your SearchCursor loop, you should be able to loop through each field (or limit to string fields) with ListFields to test the value against your string. I have no idea if this is the fastest method or not.
0 Kudos
BenYoungs
New Contributor II
Inside your SearchCursor loop, you should be able to loop through each field (or limit to string fields) with ListFields to test the value against your string. I have no idea if this is the fastest method or not.


That was the first thing I tried. The problem that I was having was looping through each field for every row of the table:

for field in fieldlist:
     if row.field == searchstring
          # do something

When I run this I get the following error:

'Row: Field field does not exist'

So I can't use the field variable with the row. statement to search for a value. I'm not sure how I can get around that.
0 Kudos
DarrenWiens2
MVP Honored Contributor
I think you need to use getValue and setValue to work with field names as variables (not sure though).
0 Kudos
LeighHolcombe
New Contributor
I think this is what you're looking for.  This is a simple find and replace routine that will loop through every field of every row in your shapefile/feature class and make the necessary changes.

x = the value you want to find
y = the value you want to replace it with
filename = the file you're working on

rows = arcpy.UpdateCursor(filename)
fields = arcpy.ListFields(filename)
for row in rows:
    for field in fields:
        value = row.getValue(field)
        if value == x:
            row.setValue(field,y)
            rows.updateRow(row)


Hopefully, that at least gets you pointed in the right direction.
0 Kudos
BenYoungs
New Contributor II
I think you need to use getValue and setValue to work with field names as variables (not sure though).


Thanks, that was part of my problem. I wasnt using the getValue, and I also was trying to use the field describe object instead of the field name specifically.

for field in fieldlist:
     fieldname = field.Name
     cell value = row.GetValue(field)

Thats what worked for me. Thanks again for the help.
0 Kudos
BenYoungs
New Contributor II
Leigh,

That is essentially what I ended up doing, except your code looks a little cleaner. Thanks for the help!
0 Kudos