Select to view content in your preferred language

For loop through UpdateCursor stops after first row

504
2
05-25-2012 04:48 PM
JaredMorante
New Contributor
I am running this script here to try and match some various fields within the same file. So I have setup a search and an update cursor within that file.

However, when I run the script, the main part of the script where the update cursor for loop exists, stops after the first entry. Clearly I am doing something wrong. Is this just a problem with trying to run two cursors in the same file? I figured it would not be a problem since the search cursor should not try and get any kind of lock on the data. Any help would be much appreciated. Code is below.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
#municLayer = arcpy.mapping.(mxd, "MUNICPIOS")
municLayer = arcpy.UpdateCursor("C:\File")
municSearch = arcpy.SearchCursor("C:\File")

def matchMunic(municipalityname):
    for row in municSearch:
        munictomatch = strip_accents(row.NOM_MUN.upper())
        enttomatch = strip_accents(row.NOM_ENT.upper()[:3])
        stringtomatch = munictomatch+" ("+enttomatch+")"
     print stringtomatch+" =? "+municipalityname
        if municipalityname == stringtomatch:
            print "We have found a match for " + stringtomatch
            municSearch.reset()
            return row.Count
    
    municSearch.reset()
    return 303
            
def strip_accents(string):
    import unicodedata
    return unicodedata.normalize('NFKD', unicode(string)).encode('ASCII', 'ignore')        

for row in municLayer:
    municname = row.Munic
    row.MunicPytho = matchMunic(municname)
    municLayer.updateRow(row)
Tags (2)
0 Kudos
2 Replies
MathewCoyle
Honored Contributor
I'm not sure I follow what you are doing exactly. I wouldn't use multiple cursors on the same dataset, I can't imagine that would ever work. I would use your search cursor to build a dictionary or list of the values you want to compute with your update cursor.
0 Kudos
DarrenWiens2
MVP Alum
Use forward slashes (/), double backslashes (\\), or raw strings (r"...") in your file paths.
0 Kudos