Using a Variable in a Search Cursor with Python

1867
5
Jump to solution
12-31-2012 07:30 AM
WinnKetchum
New Contributor III
Ok, so I have been having a problem setting up a search cursor with python that uses a variable as the desired value.  It seems that I am missing an operator, however, I am lost as to what operator I am missing.  Below is a simplified version of the code that demonstrates the issue:

[INDENT]import arcpy as arc
arc.env.workspace = "M:\GIS Mapping Services\Promotional\Automated Reports\ImportExportFieldData.mdb"

b = "Clausen Ranch Unit 34-70 29-1H"
query = "'"+"Project_Name"+"=""'"+b+"'"+"'"
rows = arc.SearchCursor("Inspection_Location",query)
for row in rows:
    [INDENT]print row.Inspection_Location, row.Inspection_Type, row.Project_Name[/INDENT][/INDENT]


And here is the error message I am getting:

[INDENT]Traceback (most recent call last):
  File "M:/GIS Mapping Services/Promotional/Automated Reports/SearchCursorWithVariable.py", line 6, in <module>
    rows = arc.SearchCursor("Inspection_Location",query)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 820, in SearchCursor
    return gp.searchCursor(*args)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 357, in searchCursor
    self._gp.SearchCursor(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.
General function failure [Inspection_Location]
Syntax error (missing operator) in query expression '( 'Project_Name='Clausen Ranch Unit 34-70 29-1H'' )'.[/INDENT]


Also, if I took the b variable and put into a list and then called the variable from the list, would that be any different than what I was attempting to accomplish above? For instance:

[INDENT]list = []
for i in list:
[INDENT]query = "'"+"Project_Name"+"=""'"+listvalue+"'"+"'"
rows = arc.SearchCursor("Inspection_Location",query)
for row in rows:
    [INDENT]print row.Inspection_Location, row.Inspection_Type, row.Project_Name[/INDENT] [/INDENT]


Thank you for any help in solving this problem.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
WinnKetchum
New Contributor III
Ok, I finally figured it out.  I found this thread which helps, although now I just need to figure out what the language means: http://forums.arcgis.com/threads/66534-Select-from-user-input-variable?highlight=search+cursor+varia....

Essentially the code looks like:
[INDENT] b = 'Clausen Ranch Unit 34-70 29-1H'
query = "Project_Name = '%s'" % (b)[/INDENT]

where the %s provides the space to input the desired variable, in this case b. 

Tested it using the second part of my original post (i as the variable taken from a list), and that works as well.  Thank you again for your posts @mvolz47 and I will try your other suggestions as well to see if they work.

View solution in original post

0 Kudos
5 Replies
MichaelVolz
Esteemed Contributor
Here is some sample code from ESRI's documentation:

import arcpy

fc = "D:/St_Johns/data.mdb/roads"

# Create a search cursor using an SQL expression
#
rows = arcpy.SearchCursor(fc, "[roadclass] = 2")
for row in rows:
    # Print the name of the residential road
    #
    print row.name

You are using a personal geodatabase as well, so you might need square brackets for your field name

query = "'"+"[Project_Name]"+"=""'"+b+"'"+"'"
0 Kudos
WinnKetchum
New Contributor III
@mvolz47,

Thanks for the reply.  Unfortunately I am still getting the same error message.
0 Kudos
MichaelVolz
Esteemed Contributor
I would try 2 further options.

1.) Create this query in ArcMap to see where you need the "".

2.) query = "'"+"[Project_Name]="+b+"'"
0 Kudos
WinnKetchum
New Contributor III
Ok, I finally figured it out.  I found this thread which helps, although now I just need to figure out what the language means: http://forums.arcgis.com/threads/66534-Select-from-user-input-variable?highlight=search+cursor+varia....

Essentially the code looks like:
[INDENT] b = 'Clausen Ranch Unit 34-70 29-1H'
query = "Project_Name = '%s'" % (b)[/INDENT]

where the %s provides the space to input the desired variable, in this case b. 

Tested it using the second part of my original post (i as the variable taken from a list), and that works as well.  Thank you again for your posts @mvolz47 and I will try your other suggestions as well to see if they work.
0 Kudos
WinnKetchum
New Contributor III
@mvolz47,

Tried option #2 from your second post, and although it gave me no errors and seemed to print my results, it printed all data from the database and not just the data I was after using the search cursor.  Not sure why it happened that way, but thank you again for the suggestions.

Cheers,

Winn
0 Kudos