1

This question already has an answer here:

I am trying to find whether any feature classes in multiple gdb's has Feature class representation. If a feature class has represenation, "RuleID" & "override" fields will be in the attribute table. So i am just checking whether these two fields are present in the attribute tables.

This code is printing the results. But also returning few errors. Don't know whats the problem.

import arcpy

def FindField(fc,myField):
        fieldList = arcpy.ListFields(fc)
        for field in fieldList:
            if str.lower(str(field.name)) == str.lower(myField):
                print "    " + fc + " contains fieldname: " + myField

def FindField2(fc,myField2):
        fieldList = arcpy.ListFields(fc)
        for field in fieldList:
            if str.lower(str(field.name)) == str.lower(myField2):
                print "    " + fc + " contains fieldname: " + myField2


myField = "RuleID"
myField2 = "Override"
dir = r'D:\Test\gdb'
arcpy.env.workspace = dir

gdbList = arcpy.ListWorkspaces('*','FileGDB')

for gdb in gdbList:
    arcpy.env.workspace = gdb               
    datasetList = arcpy.ListDatasets('*','Feature')     
    fieldList = arcpy.ListFeatureClasses()         
    for fc in fieldList:
        print arcpy.env.workspace,fc
        print "Searching root level Featureclasses..."
        print "  Searching " + fc
        FindField(fc,myField)
        FindField(fc,myField2)

    for dataset in datasetList:
        arcpy.env.workspace = dataset   
        fieldList = arcpy.ListFeatureClasses()     
        for fc in fieldList:
            print arcpy.env.workspace,fc
            print "  Searching Featureclass... " + fc
            FindField(fc,myField)
            FindField(fc,myField2)

D:\Test\gdb\RT_FactGeology_Qld_25K.gdb Rio_FactGeology_Qld_L
Searching root level Featureclasses...
Searching Rio_FactGeology_Qld_L
Rio_FactGeology_Qld_L contains fieldname: RuleID
Rio_FactGeology_Qld_L contains fieldname: Override

D:\Test\gdb\RT_Roads_Qld_25K.gdb Rio_Roads_Qld_L
Searching root level Featureclasses...
Searching Rio_FactGeology_Qld_L
Rio_Roads_Qld_L contains fieldname: RuleID
Rio_Roads_Qld_L contains fieldname: Override

Runtime error 
Traceback (most recent call last):
File "<string>", line 31, in <module>
File "<string>", line 6, in FindField
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 7: ordinal not in range(128)

marked as duplicate by Andre Silva, Jochen Schwarze, whyzar, PolyGeo arcpy Mar 14 at 21:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    One of your field names has an accented character I think, something like ó and it's throwing a Unicode error. Try using a wildcard on your Listfields to limit it to just your two desired fields and then just check that it has returned more than nothing, that'll be one that contains your field without having to compare field names – Midavalo May 5 '16 at 10:55
  • 2
    This isn't a GIS error so much as a Python one (and a Python 2.7 one at that). Have you tried researching Unicode codec errors? Please provide an MCVE (emphasis on minimal) so folks don't have count 31 lines to find your error. – Vince May 5 '16 at 11:02
  • 1
    +Midavalo Thanks a lot. I've just used myField, myField2 as wildcards. Worked like a charm – joseph_k May 5 '16 at 11:28

Browse other questions tagged or ask your own question.