I'm trying to use UpdateCursor in arcpy, but the where clause I'm using seems to be causing trouble. However, when I open up the table in ArcMap and use Select By Attributes with the exact same clause that causes an error in the Python script, it works fine.

The name of a feature class and a dictionary containing strings is passed into a function with the following code:

rows = arcpy.da.UpdateCursor(featureClass, "{0} = {1}".format("PropCode", "'"+hotelDict["hotelId"]+"'"))
for row in rows:
    print "stuff" #I just wanted to see if it was making it here

I get the following error: RuntimeError: Cannot find field 'PropCode = 'FR066''

More info that may or may not be helpful: I'm using ArcGIS 10.1, the field that is being searched in contains text, and using arcpy.AddFiedlDelimiters(featureClass, "PropCode") instead of just "PropCode" did not fix the problem.

There are no results from Googling "RuntimeError: Cannot find field", and PropCode = 'FR066' is correct when using Select By Attributes in ArcMap, so I'm kind of stumped.


Using the API wrong. arcpy.da's second argument is a list of fields, not a where clause. Did you mean:

cursor = arcpy.da.UpdateCursor(featureClass,
                               "{0} = '{1}'".format("PropCode",       
  • Thanks! I think I was accidentally looking at the 10.0 reference when I was figuring out parameters.
    – Alexandra
    Aug 3 '12 at 16:30

ArcGIS expects field names to be bounded by the double quote character: " Of course, this is also the same character in Python that separates strings. To make Python not end a string when it encounters a double quote, you need to use the backslash escape character: \. So your cursor expression will look like this:

rows = arcpy.UpdateCursor(featureClass, "\"PropCode\" = '"+hotelDict["hotelId"]+"'")

With this, the second and third double quotes are ignored by Python, and your expression is successfully passed into ArcGIS.

  • I think the delimiters for field names actually depend on how the data is stored, and in this case double quotes aren't necessary. Thank you though!
    – Alexandra
    Aug 3 '12 at 16:32
  • This answer is referring to arcpy.UpdateCursor() while the question is about arcpy.da.UpdateCursor().
    – PolyGeo
    Aug 21 '15 at 2:13

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.