<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using a Variable in a Search Cursor with Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407671#M32076</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;@mvolz47, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; Not sure why it happened that way, but thank you again for the suggestions.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Winn&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 31 Dec 2012 17:04:22 GMT</pubDate>
    <dc:creator>WinnKetchum</dc:creator>
    <dc:date>2012-12-31T17:04:22Z</dc:date>
    <item>
      <title>Using a Variable in a Search Cursor with Python</title>
      <link>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407666#M32071</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, so I have been having a problem setting up a search cursor with python that uses a variable as the desired value.&amp;nbsp; It seems that I am missing an operator, however, I am lost as to what operator I am missing.&amp;nbsp; Below is a simplified version of the code that demonstrates the issue:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]import arcpy as arc&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arc.env.workspace = "M:\GIS Mapping Services\Promotional\Automated Reports\ImportExportFieldData.mdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;b = "Clausen Ranch Unit 34-70 29-1H"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;query = "'"+"Project_Name"+"=""'"+b+"'"+"'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = arc.SearchCursor("Inspection_Location",query)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [INDENT]print row.Inspection_Location, row.Inspection_Type, row.Project_Name[/INDENT][/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And here is the error message I am getting:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "M:/GIS Mapping Services/Promotional/Automated Reports/SearchCursorWithVariable.py", line 6, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arc.SearchCursor("Inspection_Location",query)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 820, in SearchCursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return gp.searchCursor(*args)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 357, in searchCursor&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self._gp.SearchCursor(*gp_fixargs(args)))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RuntimeError: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;General function failure [Inspection_Location]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Syntax error (missing operator) in query expression '( 'Project_Name='Clausen Ranch Unit 34-70 29-1H'' )'.[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]list = []&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for i in list:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]query = "'"+"Project_Name"+"=""'"+listvalue+"'"+"'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = arc.SearchCursor("Inspection_Location",query)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [INDENT]print row.Inspection_Location, row.Inspection_Type, row.Project_Name[/INDENT] [/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you for any help in solving this problem.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Dec 2012 15:30:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407666#M32071</guid>
      <dc:creator>WinnKetchum</dc:creator>
      <dc:date>2012-12-31T15:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Variable in a Search Cursor with Python</title>
      <link>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407667#M32072</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here is some sample code from ESRI's documentation:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;fc = "D:/St_Johns/data.mdb/roads"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Create a search cursor using an SQL expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;#&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = arcpy.SearchCursor(fc, "[roadclass] = 2")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Print the name of the residential road&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.name&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You are using a personal geodatabase as well, so you might need square brackets for your field name&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;query = "'"+"[Project_Name]"+"=""'"+b+"'"+"'"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Dec 2012 16:21:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407667#M32072</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2012-12-31T16:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Variable in a Search Cursor with Python</title>
      <link>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407668#M32073</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;@mvolz47, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the reply.&amp;nbsp; Unfortunately I am still getting the same error message.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Dec 2012 16:38:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407668#M32073</guid>
      <dc:creator>WinnKetchum</dc:creator>
      <dc:date>2012-12-31T16:38:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Variable in a Search Cursor with Python</title>
      <link>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407669#M32074</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I would try 2 further options.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1.) Create this query in ArcMap to see where you need the "".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2.) query = "'"+"[Project_Name]="+b+"'"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Dec 2012 16:55:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407669#M32074</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2012-12-31T16:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Variable in a Search Cursor with Python</title>
      <link>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407670#M32075</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Ok, I finally figured it out.&amp;nbsp; I found this thread which helps, although now I just need to figure out what the language means: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.arcgis.com/threads/66534-Select-from-user-input-variable?highlight=search+cursor+variable" rel="nofollow" target="_blank"&gt;http://forums.arcgis.com/threads/66534-Select-from-user-input-variable?highlight=search+cursor+variable&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Essentially the code looks like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT] b = 'Clausen Ranch Unit 34-70 29-1H'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;query = "Project_Name = '%s'" % (b)[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;where the %s provides the space to input the desired variable, in this case b.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Tested it using the second part of my original post (i as the variable taken from a list), and that works as well.&amp;nbsp; Thank you again for your posts @mvolz47 and I will try your other suggestions as well to see if they work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Dec 2012 16:59:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407670#M32075</guid>
      <dc:creator>WinnKetchum</dc:creator>
      <dc:date>2012-12-31T16:59:16Z</dc:date>
    </item>
    <item>
      <title>Re: Using a Variable in a Search Cursor with Python</title>
      <link>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407671#M32076</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;@mvolz47, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&amp;nbsp; Not sure why it happened that way, but thank you again for the suggestions.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Winn&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Dec 2012 17:04:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-a-variable-in-a-search-cursor-with-python/m-p/407671#M32076</guid>
      <dc:creator>WinnKetchum</dc:creator>
      <dc:date>2012-12-31T17:04:22Z</dc:date>
    </item>
  </channel>
</rss>

