Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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. Any thoughts would be appreciated...

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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",       
                                                    hotelDict["hotelId"]))
share|improve this answer
    
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.

share|improve this answer
    
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

Your Answer

 
discard

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.