The script does not work

1002
6
Jump to solution
02-28-2021 07:16 AM
Sergey_Grechkin
New Contributor II

I'm trying to write a script that will check a column for data compliance with domain values

If the data does not match, then the column name and row identification number are printed

 

fc = 'D:\Arcgis\New Personal Geodatabase.mdb\TEST'
domain_2 = arcpy.da.ListDomains("D:\Arcgis\New Personal Geodatabase.mdb")
field_1 = 'ID_COD'
fields = arcpy.ListFields(fc)
cursor = arcpy.SearchCursor(fc)

for field in fields:
    if field.domain:
        print field.name
        for domain in domain_2:
            if field.domain == domain.name:
                for val in domain.codedValues.keys():
                    a=set()
                    a.add(val)
                    for row in cursor:
                        b=row.getValue(field.name)
                        if b not in a:
                            print (row.getValue(field_1))

 

But for some reason, the script does not work correctly: the first column shows all erroneously filled cells and all the rest only the column name:

STATUS #column name
1111111 #line identification number 
2222222 #line identification number 
COMPANY_MANAGER
COMPANY

Help find the error

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

I've not had great experience of this so I may be wrong, but I believe that because the cursor was instantiated outside of the loop, it is effectively stuck at the end of it's iteration.

Possibly adding cursor.reset() will do the trick. 

fc = 'D:\Arcgis\New Personal Geodatabase.mdb\TEST'
domain_2 = arcpy.da.ListDomains("D:\Arcgis\New Personal Geodatabase.mdb")
field_1 = 'ID_COD'
fields = arcpy.ListFields(fc)
cursor = arcpy.SearchCursor(fc)

for field in fields:
    if field.domain:
        print field.name
        for domain in domain_2:
            if field.domain == domain.name:
                for val in domain.codedValues.keys():
                    a=set()
                    a.add(val)
                    for row in cursor:
                        b=row.getValue(field.name)
                        if b not in a:
                            print (row.getValue(field_1))
                    cursor.reset()

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

You are using the original ArcPy cursors.  I encourage you to use the ArcPy Data Access cursors which are much more efficient and Pythonic in form:  SearchCursor - Help | Documentation

 You are checking existing record values for compliance with existing domain values.  What do you want to do if there are non-compliant record values?  Right now your code appears to be just be printing an identifier value, ID_COD.

Sergey_Grechkin
New Contributor II

my code prints the ID code of the line in which the value does not match the domain values

0 Kudos
DavidPike
MVP Frequent Contributor

I've not had great experience of this so I may be wrong, but I believe that because the cursor was instantiated outside of the loop, it is effectively stuck at the end of it's iteration.

Possibly adding cursor.reset() will do the trick. 

fc = 'D:\Arcgis\New Personal Geodatabase.mdb\TEST'
domain_2 = arcpy.da.ListDomains("D:\Arcgis\New Personal Geodatabase.mdb")
field_1 = 'ID_COD'
fields = arcpy.ListFields(fc)
cursor = arcpy.SearchCursor(fc)

for field in fields:
    if field.domain:
        print field.name
        for domain in domain_2:
            if field.domain == domain.name:
                for val in domain.codedValues.keys():
                    a=set()
                    a.add(val)
                    for row in cursor:
                        b=row.getValue(field.name)
                        if b not in a:
                            print (row.getValue(field_1))
                    cursor.reset()
Sergey_Grechkin
New Contributor II

David you are right 🙂

cursor = arcpy.SearchCursor(fc)

need to be placed in the body of the loop

 

 

fc = 'D:\Arcgis\New Personal Geodatabase.mdb\TEST'
domain_2 = arcpy.da.ListDomains("D:\Arcgis\New Personal Geodatabase.mdb")
field_1 = 'ID_COD'
fields = arcpy.ListFields(fc)
 
for field in fields:
    if field.domain:
        print field.name
        for domain in domain_2:
            if field.domain == domain.name:
                for val in domain.codedValues.keys():
                    a=set()
                    a.add(val)
                    cursor = arcpy.SearchCursor(fc)
                    for row in cursor:
                        b=row.getValue(field.name)
                        if b not in a:
                            print (row.getValue(field_1))

 

 

Result:

STATUS
1111111
2222222
COMPANY_MANAGER
1111111
33333333
COMPANY
2222222

DanPatterson
MVP Esteemed Contributor

You are doing this in arcmap.  You should raw encode your file paths since it will cause errors going forward

f = "D:\Arcgis\New Personal Geodatabase.mdb\TEST"  # --- won't work in ArcGIS Pro
  File "<ipython-input-4-a546e0cc6769>", line 1
    f = "D:\Arcgis\New Personal Geodatabase.mdb\TEST"
       ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 9-10: malformed \N character escape


f = r"D:\Arcgis\New Personal Geodatabase.mdb\TEST" # --- raw encoding

... sort of retired...
0 Kudos
by Anonymous User
Not applicable

I'd suggest you step through the code with a debugger so you can watch what your variables become and what your code is doing throughout its loops.

...

Since domain_2 is a list, you can use list comprehension to check for equality by changing

if field.domain:
    print field.name
    for domain in domain_2:
        if field.domain == domain.name:

to

if field.domain in [domain.name for domain in domain_2]:
   print field.name