Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know this question has been answered but I am not seeing an adaptation that I can understand. I am trying to use a numeric variable in a where clause and just having a hard time figuring it out. Your help is much appreciated

fc1 = "L:\ENGINEERING\collind\ArcGIS\Default.gdb\sedaSampleHistory"

fieldname = "pocid"

delimfield = arcpy.AddFieldDelimiters(fc1, fieldname)

pocidCnt = 3

cursor = arcpy.da.SearchCursor(fc1, ["pocid", "cl2total"], delimfield + " = pocidCnt ")

I have tried all kinds of ways to get pocidCnt to work in the where clause and I am not having any luck. If I write it as

cursor = arcpy.da.SearchCursor(fc1, ["pocid", "cl2total"], delimfield + " = 3 ")

it works fine. What am I doing wrong?

share|improve this question
Have you tried not passing it as a string in quotes and just putting the variable name with no quotes:-------cursor = arcpy.da.SearchCursor(fc1, ["pocid", "cl2total"], delimfield + pocidCnt ) – Chris R Aug 22 '14 at 17:02
Thanks for the quick response... when I make that change I get Runtime error Traceback (most recent call last): File "<string>", line 10, in <module> TypeError: coercing to Unicode: need string or buffer, int found – cdd Aug 22 '14 at 17:08
up vote 5 down vote accepted

Your code fails because the variable name (pocidCnt) is inside a string and is therefore being taken literally: the final query being used is "pocid" = pocidCnt, where pocidCnt is clearly not a number that can be matched. You can substitute the value represented by the pocidCnt variable into the query string in the following ways:

cursor = arcpy.da.SearchCursor(fc1, ["pocid", "cl2total"], delimfield + " = " + str(pocidCnt))

or, preferably:

cursor = arcpy.da.SearchCursor(fc1, ["pocid", "cl2total"], "{0} = {1}".format(delimfield, pocidCnt))
share|improve this answer
Thank you. Both of those work. So I guess what I was trying to do was insert an int into an expression that is a string. Is that correct? – cdd Aug 22 '14 at 17:13
Yes, the value of pocidCnt must be converted from an int to a string (i.e. from 3 to "3") before it can be combined with the rest of the query string. The str() function achieves this clumsily, while the string .format() method is a little more graceful about it. – nmpeterson Aug 22 '14 at 17:16
Thank you very much for the help and explanation! – cdd Aug 22 '14 at 18:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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