I am writing a script that cycles through several different point shapefiles, finds how many lie within a given area and the writes the outcome to a table in an Access database. I've used an search cursor to look up the point file names, which is then saved to a variable. I'd then like to use this variable to specify which field to update in another table. Here is my code:

selection = 1
while selection < 3:
        rows = arcpy.SearchCursor("C:\\MyDB.mdb\\Source_Class_combinations") #opens search cursor on the info table
        for row in rows:
            if row.Order == selection:  
                destfield = row.DestField # extracts the name of the destination field
        del row  
        del rows 
        print "Defining variables..."
        ptfile = str(destfield)+"_lyr"
        rowtoud = "row."+str(destfield) #create the variable with the field to update

        # Process: Select Layer By Location
        print "select by location"
        arcpy.SelectLayerByLocation_management(ptfile, "INTERSECT", neighfile, "", "NEW_SELECTION")
        count = int(arcpy.GetCount_management(ptfile).getOutput(0))
        print "count = " + str(count)

        #Update the number of points selected in the neighbourhoods table
        print "updating count..."
        rows = arcpy.UpdateCursor("C:\\MyDB.mdb\\neighbourhoods") 
        for row in rows:
            if row.StudyID == str(neigh):
                rowtoud = count
        del rows
        del row
        selection +=1

The selection all seems to work fine, but the table is not updated and no error message is returned. Can I use a variable like this?

I'm using ArcGIS 10, python 2.6, and Access 2007

Thanks, Flo

  • ah ha, that works! but I don't think I can accept a comment as an answer to the question, can I? I guess it's a moot point but is there a way that you can make the the other method work? – Flo Harrison Oct 1 '12 at 13:47
  • I would try debugging by printing your variable out. Since the variable only stores the string row.FieldName I can't imagine why it wouldn't work -- maybe there's a simple error that you didn't catch? In any case go with what works! – Roy Oct 1 '12 at 14:10
  • I did have a line which printed the variable, and it looked fine, but when I ran the script: rowtoud = count, it re-sets the 'rowtoud' variable to the same value as the 'count' variable. anyway, as you say, I'll use the method that works! – Flo Harrison Oct 1 '12 at 14:20
  • 3
    The row.FieldName = x method is just a shortcut to row.setValue("FieldName", x), you cannot use a variable for the field with the row.FieldName syntax. – blah238 Oct 1 '12 at 17:08

Try using the setValue(fieldName, object) method with your field name variable.

|improve this answer|||||

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.