Read one field and apply domain value to another field with UpdateCursor: KeyError

897
11
Jump to solution
03-28-2023 09:30 AM
nsidaniel
New Contributor III

 

I'm trying to read the [OWN] field and apply a domain value to 3 different fields with UpdateCursor, yet I keep getting a KeyError:

KeyError                                  Traceback (most recent call last)
In  [84]:
Line 49:    row[0] = lookup_values['Fee']

KeyError: 'Fee'

 

The domains are already set and show up correctly on the a POLYGONS_feature_class. I'm sick of staring at this bit of code! Any ideas?

 

 # defines OpCo, RightsType, and Type_of_Use fields 
    # by info in OWN field

gdb = projectfilepath + projectfileGDB
fc = POLYGONS_feature_class
domains = arcpy.da.ListDomains(gdb)

 # 'OpCo' field
cur = arcpy.UpdateCursor(fc)
for row in cur:
    if row.getValue('OWN') == 'INT %' or 'IT %' or 'TRANSMISSION, TAX DEPARTMENT':       
        fieldname = 'OpCo'
        field = arcpy.ListFields(fc, fieldname)[0]
        for domain in domains:
            if domain.name == field.domain:
                coded_values = domain.codedValues
                lookup_values = dict(zip(coded_values.values(),coded_values.keys()))
                break
# Set 'OpCo' field
        with arcpy.da.UpdateCursor(fc, fieldname) as cursor:
            for row in cursor:
                row[0] = lookup_values['IT']
                cursor.updateRow(row)


# 'RightsType' field  
cur = arcpy.UpdateCursor(fc)
for row in cur:
    if row.getValue('OWN') == 'INT %' or 'IT %' or 'TRANSMISSION, TAX DEPARTMENT':
        fieldname = 'RightsType'              
        field = arcpy.ListFields(fc, fieldname)[0]
        for domain in domains:
            if domain.name == field.domain:
                coded_values = domain.codedValues
                lookup_values = dict(zip(coded_values.values(),coded_values.keys()))
                break
 # Set 'RightsType' field 
        with arcpy.da.UpdateCursor(fc, fieldname) as cursor:
            for row in cursor:
                row[0] = lookup_values['Fee']
                cursor.updateRow(row)   
                
 
 # 'Type_of_Use' field
cur = arcpy.UpdateCursor(fc)
for row in cur:
    if row.getValue('OWN') == 'INT %' or 'IT %' or 'TRANSMISSION, TAX DEPARTMENT': 
        fieldname = 'Type_of_Use'
        field = arcpy.ListFields(fc, fieldname)[0]
        for domain in domains:
            if domain.name == field.domain:
                coded_values = domain.codedValues
                lookup_values = dict(zip(coded_values.values(),coded_values.keys()))
                break
 # Set Type_of_Use field 
        with arcpy.da.UpdateCursor(fc, fieldname) as cursor:
            for row in cursor:
                row[0] = lookup_values['Full T-Line']
                cursor.updateRow(row)

 

0 Kudos
11 Replies
BlakeTerhune
MVP Regular Contributor

Like this then?

fields = ["OWN1", "OpCo", "RightsType", "Type_of_Use"]
with arcpy.da.UpdateCursor(fc, fields) as u_cursor:
    for own1, opco, rightstype, type_of_use in u_cursor:
        if own1 in ("ITC", "TRANSMISSION, TAX DEPARTMENT"):
            opco = "ITCT"
        elif own1 == "CONSUMERS":
            opco = "METC"
        u_cursor.updateRow([own1, opco, rightstype, type_of_use])

 

nsidaniel
New Contributor III

Yes! That's it! Thank you @BlakeTerhune

0 Kudos