4

How would you wildcard the field_names for a SearchCursor so that it only searched through rows for columns in a table that begin with the word "Layer"? For example:

FCList = []
FCS = [u'Building', u'Bridges', u'RecreationArea']
LCC = "G:\\CL.gdb\\Layer_CatCode"
with arcpy.da.SearchCursor(LCC, ['Layer*']) as cursor:
    for row in cursor:
        if row[0] in FCS and row[0] not in FCList:
            FCList.append(row[0])

I have tried to construct a sql statement for the field_names with no success, and I have even tried the asterisk option for field_names so that is just searches all columns but the FCList comes out empty. However, this may be due to the fact that when using the asterisk option I'm not sure what to put in the brackets for row["*"].

1
  • 1
    It's recommended that you add tags to your question to indicate exactly what software and version you are using.
    – Tom
    Commented Aug 9, 2016 at 16:08

1 Answer 1

5

The cursor takes a list/tuple of field names--not a wildcard. The wildcard can be used to get that list of field names. It's easiest to make it a separate statement.

FCList = []
FCS = [u'Building', u'Bridges', u'RecreationArea']
LCC = "G:\\CL.gdb\\Layer_CatCode"
fields_cur = [i.name for i in arcpy.ListFields(LCC, 'Layer*')]
with arcpy.da.SearchCursor(LCC, fields_cur) as cursor:
    for row in cursor:
        if row[0] in FCS and row[0] not in FCList:
            FCList.append(row[0])
2
  • Thank you, this worked. I had actually tried using a separate ListFields statement but apparently I was constructing it incorrectly.
    – DanMilRiv
    Commented Aug 9, 2016 at 16:16
  • @DanMilRiv, yeah, ESRI is strangely inconsistent in their design of ArcPy; the ListFields function returns field objects, while the ListFeatureClasses function returns strings. You'd think they could write a tiny bit of code within the cursor object to parse the passed fields and interpret them correctly whether they are field names or field objects. But, no. This sort of thing throws one off quite a bit. That's why it's usually best just to leave open the ArcPy help site so you can constantly reference the documentation to catch these--I'll be nice and call them "quirks".
    – Tom
    Commented Aug 9, 2016 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.