getting the field name in searchcursor through variable

2099
2
04-23-2013 08:02 AM
StephenKruzik
New Contributor III
I am doing a bulk spatial join on gps points by assigning the attributes within a site buffer to the points themselves.  Unfortunately, there are 4 different types of GPS points.  Within that, I only want to populate the most frequently occurring points(categories).  I have it set up with a stats table ordered by frequency.  From there, I am inserting the top 3 into a list, and then assigning as such.

My problem is that because the ID field for the GPS points has a different name for each type.  When I go to grab the field from the search cursor, I'm forced to use an item.{variable} rather than item.fieldname.  It doesn't like that at all!  Is there a better way I could grab the field name from the table?

Any help would be greatly appreciated.  I know it's a lot of code, but as I said, I'm just trying to find a way to grab the item.Columnname value from within the cursor.


 

towerfield = ["RNW","Cell","Scramble1","ServSN"]

def PQid():
    for value in towerfield:
        print towerfield.index(value),value
    PQnum = input("Which field identifier is this carrier using?")
    PQKey = towerfield[PQnum]
    return PQKey
Columnname = PQid()
arcpy.Statistics_analysis("FC_lyr", "F:/GIS Data/Drive Testing.gdb/stats", [["Device","COUNT"]], Columnname) #draw stats from inside feature
    statstable = "F:/GIS Data/Drive Testing.gdb/stats"
    result = int(arcpy.GetCount_management(statstable).getOutput(0))
    print result
    Scursor = arcpy.SearchCursor("F:/GIS Data/Drive Testing.gdb/stats")#, "","", Statscolumn +  "; \"FREQUENCY\"", "COUNT_Device") #allow searching of stats table
    currentState = "" 
    mylist = []
    Scursorfields = arcpy.ListFields(statstable)
    for item in Scursor:
        if arcpy.Exists("SelectedSite"):
            rt = (item.Frequency, str(item.Columnname),)
            mylist.append(rt)
            mylist.sort()
            mylist.reverse()
Tags (2)
0 Kudos
2 Replies
ChrisSnyder
Regular Contributor III
If you are using the old cursor model (the non-data access kind), you will need to use the .getValue method if your field name is a variable. So for example:

fieldName = "TEST"
valueList = []
searchRows = arcpy.SearchCursor(myFC)
for searchRow in searchRows:
    valueList.append(searchRow.getValue(fieldName))
del searchRow, searchRows


If you want to use the newer data access cursor model (only available in v10.1+):

fieldName = "TEST"
valueList = []
searchRows = arcpy.da.SearchCursor(myFC,["*"])
for searchRow in searchRows:
    valueList.append(searchRow[searchRows.fields.index(fieldName)])
del searchRow, searchRows
0 Kudos
StephenKruzik
New Contributor III
getValue huh? 
well sir; you are correct.

I am currently still on 10.0, so I did have to use the previous methods.  Worked like a charm though.  Thanks so much!
0 Kudos