Problems using the where clause with searchcursor method

8188
9
03-01-2011 09:25 PM
KevinCressy
New Contributor II
Hello all,

I am using ArcGIS Desktop 10.0 and I am  trying to write a script in python.

I am having problems with the where clause in the searchcursor method.

Basically I have a CAD file and would like to identify from the CAD file all of the Annotations that have been flagged with a  ??????? question mark.

So if I have the following list of Annotations in the Text filed of the Annotation CAD feature class, I would like to pull out the 3rd 5th and 6th rows using search cursor:

33334508
33304576
?97464800
64538300
?74537390
?64849300

I have managed to get the where clause in the search cursor to identify individual records but it keeps failing.  I have attached an example of the CAD file and also the script.

I believe that the problem may be that the ???question mark??? is confusing the where clause.  I have tried to escape the non standard characters and also writing the where clause as a variable but do not seem to be getting anywhere.

If anyone could help it would be really appreciated, I copy my script below......

Regards,

Kevin

import arcpy
from arcpy import env
 
env.workspace = r"C:\CADfile.dwg"

#This does not work: cur = arcpy.SearchCursor("Annotation",'LEFT("Text",1)='[\\?]'',"","","")
#This works: cur = arcpy.SearchCursor("Annotation","\"Text\" = \'69570020\'")
#This works: cur = arcpy.SearchCursor("Annotation",'"Text" < \'1\'')

cur = arcpy.SearchCursor("Annotation",'\"Text\" LIKE \'\?\%\'\')

# Iterate through the rows in the cursor
for row in cur:
    print "Layer: %s,  Parcel: %s" % \
            (row.Layer, row.Text)

del cur, row
Tags (2)
0 Kudos
9 Replies
AndrewChapkowski
Esri Regular Contributor
Try not putting the field name in quotes and the like statement should work.

Here is what I did to get it to work:

import arcpy

expression = "Text LIKE '?%'"

cadFile = r"c:\temp\CADfile.dwg\Annotation"

cursor = arcpy.SearchCursor(cadFile, expression)

for row in cursor:
    print row.getValue("Text")

del row, cursor



Result:
>>> 
?69570190
?69570430
?69570440
?69570550
>>> 
0 Kudos
KevinCressy
New Contributor II
Thats great - thanks it worked!
0 Kudos
GraziaZulian
New Contributor II
hi
can you give some examples for using: cursor = arcpy.UpdateCursor(FC, expression)

with: shape files
with: geodatabase (*.gdb) FC
with: geodatabase (*.mdb) FC
with: *dbf tables

I stil have problems in preparing the where_clause for the UpdateCursor and SearchCursor.
I can't understand the syntax.
thanks
0 Kudos
AndrewChapkowski
Esri Regular Contributor
0 Kudos
LukeBehling
Occasional Contributor
Hello Andrew,
I've reviewed the links you posted over and over, and I still cannot get the UpdateCursor to work with a where clause. I am trying to take a selection and do a find and replace on only that selection. The script runs when done without the where clause; but updates the entire feature class.

# Set environment settings
env.workspace = "H:/PythonMSAG/P1_staging.mdb"

# Set local variables
inFeatures = "Address_Points"

cur3 = arcpy.UpdateCursor(inFeatures, '[CITY_ID]=\'KAUK\'')

RESULTS in this error:
Traceback (most recent call last):
  File "H:\PythonMSAG\findreplace1.py", line 72, in <module>
    del cur3, row
NameError: name 'row' is not defined

for row in cur3:
    row.MSAG_NAME = string.replace(row.MSAG_NAME, "FOURTH", "4")
    cur3.updateRow(row)
   
del cur3, row
0 Kudos
AndrewChapkowski
Esri Regular Contributor
It looks like you might have an incorrect where statement in your code:
Try this:
where =  "[CITY_ID]=\"KAUK\""
cur3 = arcpy.UpdateCursor(inFeatures, where)

0 Kudos
LukeBehling
Occasional Contributor
It still doesn't work. I've tried a Personal (9.3.1) and File geodatabase (10). It seems like no matter what I put in the where clause, it fails.
0 Kudos
AndrewChapkowski
Esri Regular Contributor
Can you use the Make Feature Layer against the where clause you are trying to use in arcmap?

The tool can be found in arctoolbox -> data management tools -> Layers and Table Views -> Make Feature Layer


Try copying and pasting that where clause for use of the update cursor.

Also, are you the only person using this dataset?  You need to be able to get an exclusive data lock on your datasets.
0 Kudos
LukeBehling
Occasional Contributor
Okay, figured it out, the attribute code was different. KAUK should have been KAUC. WHERE clause was correct.
0 Kudos