2

I'm using a unique list to populate up to 10 fields for a fc with correct geometry, in an "overloading" manner as you would in excel. I noticed on my first run through I had an "index out of range" error, which was strange as values should not populate more than 10 fields per record. I did a bit of an error check with a "break" and print, and it seems from the final result that there are duplicate values being entered where there most definitely should not be. Problem code:

for x in uniqueList:
    query = "[BAND_NUM] = \"%s\"" % x
    if x <> "" and x <> " " and x is not None:
        try:
            gp.MakeFeatureLayer("FIRST", "lyr1")
            gp.MakeFeatureLayer("END", "lyr2")
            gp.SelectLayerByAttribute("lyr1", "NEW_SELECTION", query)
            gp.SelectLayerbyLocation("lyr2", "HAVE_THEIR_CENTER_IN", "lyr1", "", "NEW_SELECTION")

            cursor = gp.UpdateCursor("lyr2")
            urow = cursor.next()
            count = 0
            print x

            # update NULL or blank fields
            while urow:
                # this was my error check
                if count == 10:
                    print count
                    break

                if urow.getValue(fieldvalues[count]) is not None and urow.getValue(fieldvalues[count]) <> "":
                    count += 1
                # go to next feature only after an update
                else:

                    urow.setValue(fieldvalues[count], x)
                    cursor.updateRow(urow)
                    count = 0
                    urow = cursor.Next()


            gp.SelectLayerByAttribute("lyr1","CLEAR_SELECTION")
            gp.SelectLayerByAttribute("lyr2","CLEAR_SELECTION")
        except arcgisscripting.ExecuteError:
            gp.AddError(gp.GetMessages(2))
        finally:
            gp.Delete("lyr1")
            gp.Delete("lyr2")
            del urow, cursor

del gp

The list looks like - fieldvalues = ['FN1', 'FN2', 'FN3', 'FN4', 'FN5', 'FN6', 'FN7', 'FN8', 'FN9', 'FN10']

I believe the problem is in the way I'm trying to loop this - my first if statement has no cursor.next() because I want to stay on the same record, at a higher index. The logic is that if the record gets updated, go to the next one in the cursor. I had something similar before where I would append one field in a comma delimited manner, but it appears I won't be using a look-up table so 10 fields is the way I'm going.

Here's an example of the duplication - pic

  • trying an alternative - while urow: for i in fieldvalues: if urow.getValue(i) is None or urow.getValue(i) == "": urow.setValue(fieldvalues[count], str(u)) cursor.updateRow(urow) urow = cursor.Next() break – Sleep6 Nov 29 '13 at 18:20
1

Solved with -

if str(u) <> "" and str(u) <> " ":
        try:
            gp.MakeFeatureLayer("FN_THEME", "lyr1")
            gp.MakeFeatureLayer("FN_THEME_Final", "lyr2")
            gp.SelectLayerByAttribute("lyr1", "NEW_SELECTION", query1)
            gp.SelectLayerbyLocation("lyr2", "HAVE_THEIR_CENTER_IN", "lyr1", "", "NEW_SELECTION")
            cursor = gp.UpdateCursor("lyr2")
            urow = cursor.Next()
            while urow:
                for i in fieldvalues:
                    if urow.getValue(i) is None or urow.getValue(i) == "":
                        urow.setValue(i, str(u))
                        cursor.updateRow(urow)
                        urow = cursor.Next()
                        break
            gp.SelectLayerByAttribute("lyr1", "CLEAR_SELECTION")
            gp.SelectLayerByAttribute("lyr2", "CLEAR_SELECTION")
        except:
            gp.AddError(gp.GetMessages(2))
        finally:
            gp.Delete("lyr1")
            gp.Delete("lyr2")
            del urow, cursor

Though Python freezes at the very end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.