AnsweredAssumed Answered

Using a Variable in a Search Cursor with Python

Question asked by wakwin on Dec 31, 2012
Latest reply on Dec 31, 2012 by wakwin
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.

Outcomes