Select to view content in your preferred language

Using python variables in SQL expression

713
2
Jump to solution
03-08-2012 01:07 PM
KevinViani
Deactivated User
I am trying to write something that describes assets with multiple locations and ranks the locations based on a road characterists.  I am basically building a list of the values that have multiple locations and then sending those values to a function that sorts and ranks those values.


#______________________________________________________________________ #define a function that takes the the assets with duplicate locations and assigns primary, secondary and so on based on the sorting of three fields #function takes the pid value and the full list of culvert locations  def setPrimLoc(pidV,CulvertLOC):     upExp="\"PID\" = " + str(pidV)     curUp = arcpy.UpdateCursor(CulvertLOC,upExp)     arcpy.Sort_management(CulvertLOC, "CULVsort", [[ "RT_TYPE","ASCENDING"], ["RT_RAMP","ASCENDING"],["RID_ETE","ASCENDING"]])     pl=1     for row in curUp:         row.PRIM_LOC = pl         pl+=1         curUp.updateRow(row)      # Delete cursor and row object     del row      del curUp   #______________________________________________________________________ #build a list of the ID numbers with multiple locations #outer loop that cycles through the circulates through the list of duplicate values #sends them to the function that sets primary location  curSch = arcpy.SearchCursor("CULVsort",MULTIPLEexpr)  pidLIST=[] for row in curSch:     if row.PID not in pidLIST:         pidLIST.append(row.PID) print pidLIST del curSch  val=0 for x in pidLIST:     print "Start outer"     pidV=pidLIST[val]     print x     sndLYR="LOC_LOAD_SGL_lyr"     exprPID="\"PID\""+"="+str(x)     arcpy.MakeFeatureLayer_management (CulvertLOC,sndLYR,exprPID)     setPrimLoc(x,sndLYR)     val+=1  


The error that I am comming up with is as follows:

       exprPID="\"PID\""+"="+str(x) TypeError: 'unicode' object is not callable 


Help me out I am beating myself up! Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Can't test this right now, so this is a shot in the dark, but I'd try replacing these two lines.
pidLIST.append(row.PID)  exprPID="\"PID\""+"="+str(x)

With these
pidLIST.append(str(row.PID))  exprPID = "\"PID\" = " + x

View solution in original post

0 Kudos
2 Replies
MathewCoyle
Honored Contributor
Can't test this right now, so this is a shot in the dark, but I'd try replacing these two lines.
pidLIST.append(row.PID)  exprPID="\"PID\""+"="+str(x)

With these
pidLIST.append(str(row.PID))  exprPID = "\"PID\" = " + x
0 Kudos
KevinViani
Deactivated User
Matt - Thanks!

I'm not sure why but when I load the int values into the list and then retrive them through a for loop, they come back out as unicode values.  I don't know enough about what I'm am doing tounderstand why, but loading them to the list as string values worked perfectly.  Thanks for the tip.
0 Kudos