Update Cursor/If Statement

1194
6
08-08-2021 09:40 PM
ThiPham12
New Contributor III

I am trying to use the Update Cursor to look-up a key in a dictionary I created, Region_dic and if it finds the key, it will update the row with the value. However, for some reason, the if statement equals false, when it should be true. Thanks for your help!

keyRegion='directory pathway'

with arcpy.da.UpdateCursor(keyRegion,["Region","ID"]

        for row in cursor:

             if row[0] in Region_dic:

                    row[1]==Region_dic.get(row[0])

                    cursor.updateRow(row)

 

0 Kudos
6 Replies
DavidPike
MVP Frequent Contributor

maybe a case issue or whitespace?  Maybe ditch the cursor for a moment and just add some print statements to debug.

broy06
by
New Contributor III

Hi, Try to use print statement before cursor.updateRow(row). so that if there is an error you'll find it.

0 Kudos
ThiPham12
New Contributor III

HI Prabal, 

Thanks for your help. When I add the else: 'not found', the result is not found. When I tried the print statement: print('row[0] not found'), there is an error "error return without exception set."

0 Kudos
JohannesBierer
Occasional Contributor III

In my opinion you will have to loop the dictionary as well to find the right key? Maybe try something like this?

with arcpy.da.UpdateCursor(keyRegion,["Region","ID"]) as cursor:

    for row in cursor:

        for key, value in Region_dic.iteritems():

            if key == row[0]:

                print key, value
                row[1] = value

                cursor.updateRow(row)

            else:
                print ("does not fit")
0 Kudos
by Anonymous User
Not applicable

The 'if row[0] in Region_dic' and Region_dict.get() methods iterate through/ lookup the dictionary already.

I'd wonder if the key types are the same between the dictionary and the row[0] value being checked as well as what @DavidPike said with Case differences.

 

 

 

with arcpy.da.UpdateCursor(keyRegion,["Region","ID"]) as cursor:
    for row in cursor:
        val = Region_dic.get(row[0], 'Not Found')  # you can set this to default to other default values (like the default None) if the key is not found in the dict.
        if val != 'Not Found': 
            row[1] = val
            cursor.updateRow(row)
        else:
            print(f'{row[0]} was not found. Check the dictionary keys for differences.')

 

 

JohannesLindner
MVP Frequent Contributor

I think you copied your code wrong, but just in case:

The first line is incomplete (closing paranthesis and "as cursor:")

row[1]==Region_dic.get(row[0])
# You're using two equal signs, which compares row[1] to Region_dic[row[0]];
# this line will compute to True or False, but it won't set row[1].

# what you want is this:
row[1] = Region_dic.get(row[0])

 

You actually don't need the manual check for the key, you can just catch the KeyError:

keyRegion = 'directory pathway'
with arcpy.da.UpdateCursor(keyRegion, ["Region","ID"]) as cursor:
    for r, i in cursor:
        try:
            i = Region_dic[r]
            cursor.updateRow([r, i])
        except KeyError:
            print("{} was not found.".format(r))

 


Have a great day!
Johannes
0 Kudos