Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Using a Variable in a Search Cursor with Python

$
0
0
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:
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:
print row.Inspection_Location, row.Inspection_Type, row.Project_Name

And here is the error message I am getting:
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'' )'.

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:
query = "'"+"Project_Name"+"=""'"+listvalue+"'"+"'"
rows = arc.SearchCursor("Inspection_Location",query)
for row in rows:
print row.Inspection_Location, row.Inspection_Type, row.Project_Name

Thank you for any help in solving this problem.

Viewing all articles
Browse latest Browse all 2485

Trending Articles