<?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: Select Layer By Attribute User Input in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444272#M34800</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Perfect - it's working fine now, thanks a lot for your help&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 09 Jan 2013 15:53:47 GMT</pubDate>
    <dc:creator>ScottMacDonald</dc:creator>
    <dc:date>2013-01-09T15:53:47Z</dc:date>
    <item>
      <title>Select Layer By Attribute User Input</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444266#M34794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have been puzzling over a script I have to write and run in an Arc toolbox and I wondered if anyone could tell me where I'm going wrong.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The following code relates to a rivers shapefile called Rio_Panuco in a geodatabase called panuco.gdb.&amp;nbsp; There is a field in the shape file called "type" and the attribute I want to select is "link".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I was able to create the file I wanted when the script was hard coded to the geodatabase using the code below-&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByAttribute_management("River_lyr", "NEW_SELECTION", '"type" = \'link\'')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, I need to write generic code and require the user to input the requirements i.e. Select * from Rio_Panuco where "type" = 'link'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have written the following code to do this (and variations galore!) but still cannot get it to work properly. I've also put the code in ArcToolbox but when the Select Layer By Attribute box is clicked it is blank.&amp;nbsp; I would be grateful for any advice.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# extract features by attrtibute&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# import system modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# get user supplied path, layers and fields&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;path is workspace&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;path = arcpy.GetParameterAsText(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# riverLayer is Rio_Panuco_Lyr Feature Layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inputLayer = arcpy.GetParameterAsText(1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# confluence is river type 'link' and is a string&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;confluence = arcpy.GetParameterAsText(2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;whereClause = '"' str(typeField) + " = '" + str(confluence) + "'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = arcpy.SearchCursor(inputLayer, whereClause)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# typeField is fieldname 'type' and is SQL expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;typeField = arcpy.GetParameterAsText(3)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# set output location&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;outputLayer = arcpy.GetParameterAsText()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# set overwrite option&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# arcp.overwriteOutput = true&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# error trapping measures&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # make a layer from the feature class&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.MakeFeatureLayer_management(inputLayer, "River_lyr")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # select layer by attribute&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.SelectLayerByAttribute_management("River_lyr", "SelectionRiverLayer", whereClause)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # write selected features to a new featureclass&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.CopyFeatures_management("River_lyr", "SelectionRiverLayer")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;except:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print arcpy.GetMessages()&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jan 2013 15:15:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444266#M34794</guid>
      <dc:creator>ScottMacDonald</dc:creator>
      <dc:date>2013-01-08T15:15:32Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Attribute User Input</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444267#M34795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What error are you getting?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Why are you using a cursor?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is a better example of string substitution creating a query string.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jan 2013 15:28:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444267#M34795</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-01-08T15:28:49Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Attribute User Input</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444268#M34796</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks mzcoyle for replying to my thread.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried substituting some of my code for the code that you sent (script below) but I couldn't get it to work.&amp;nbsp; It came up with the following error -&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;type 'exceptions.NameError'&amp;gt;: name 'path' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (tryRiver).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am also not sure why I was using a cursor - I thought I needed this to search through the fieldname (called type) for rivertypes (called link).&amp;nbsp; I'm new to scripting and all I want to do is create an Arc tool based on arcpy scripting that lets a user navigate to the feature class (Rio_Panuco) and lets them select "type" = 'link' (fieldname = attribute) and saves the feature to a new layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I've noticed that the select by attributes tool required a layer as input and not a dataset so I couldn't get that to work and I tried basic make feature layer commands.&amp;nbsp; I can make a feature layer from a feature class using user input but I cannot select only fields whose type = link and then save it to a new layer.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would appreciate any further help or suggestions on this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Code was - &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# extract features by attrtibute&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# import system modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# get user supplied path, layers and fields&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;path is workspace&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;path = arcpy.GetParameterAsText(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# riverLayer is Rio_Panuco_Lyr Feature Layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inputLayer = arcpy.GetParameterAsText(1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# confluence is river type 'link' and is a string&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;confluence = arcpy.GetParameterAsText(2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# typeField is fieldname 'type' and is SQL expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;typeField = arcpy.GetParameterAsText(3)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# set output location&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;outputLayer = arcpy.GetParameterAsText()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# set overwrite option&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# arcp.overwriteOutput = true&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# error trapping measures&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # make a layer from the feature class&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.MakeFeatureLayer_management(inputLayer, "River_lyr")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # select layer by attribute&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.SelectLayerByAttribute_management("River_lyr", "SelectionRiverLayer", whereClause)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # write selected features to a new featureclass&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.CopyFeatures_management("River_lyr", "SelectionRiverLayer")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;except:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print arcpy.GetMessages()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Kind regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Scottaidh&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jan 2013 17:04:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444268#M34796</guid>
      <dc:creator>ScottMacDonald</dc:creator>
      <dc:date>2013-01-08T17:04:51Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Attribute User Input</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444269#M34797</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This line looks like the problem, just delete it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;path is workspace&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also you can accomplish what you want to do with one tool, &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//000800000005000000" rel="nofollow noopener noreferrer" target="_blank"&gt;Select&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Something like this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# extract features by attrtibute

# import system modules
import arcpy

# get user supplied path, layers and fields
path = arcpy.GetParameterAsText(0)
arcpy.env.workspace = path

# riverLayer is Rio_Panuco_Lyr Feature Layer
inputLayer = arcpy.GetParameterAsText(1)
# confluence is river type 'link' and is a string
confluence = arcpy.GetParameterAsText(2)
whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)
# typeField is fieldname 'type' and is SQL expression
typeField = arcpy.GetParameterAsText(3)
# set output location
outputLayer = arcpy.GetParameterAsText(4)

# error trapping measures
try:

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Select_analysis(inputLayer, outputLayer, whereClause)

except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print arcpy.GetMessages()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:52:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444269#M34797</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T19:52:08Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Attribute User Input</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444270#M34798</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks again Matthew for your reply.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Unfortunately I'm still having problems.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Firstly, I removed the 'path is workspace' line but it made no difference.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I decided to try your Select_analysis suggestion but I can't seem to get the Where_clause running correctly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I get the following error message -&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;lt;type 'exceptions.NameError'&amp;gt;: name 'typeField' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (Select).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd be grateful for any advice on this as I've been trying to create this layer for days now to no avail!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Scott&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;code -&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# extract features by select analysis&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# import system modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# get user supplied path, layers and fields&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;path = arcpy.GetParameterAsText(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.workspace = path&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# riverLayer is Rio_Panuco_Lyr Feature Layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;inputLayer = arcpy.GetParameterAsText(1)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# confluence is river type 'link' and is a string&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;confluence = arcpy.GetParameterAsText(2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;whereClause = "{0} = '{1}'".format(arcpy.AddFieldDelimiters(inputLayer, typeField), confluence)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# typeField is fieldname 'type' and is SQL expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;typeField = arcpy.GetParameterAsText(3)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# set output location&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;outputLayer = arcpy.GetParameterAsText(4)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# error trapping measures&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; # make a layer from the feature class&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; arcpy.Select_analysis(inputLayer, outputLayer, whereClause)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;except:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print arcpy.GetMessages()&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jan 2013 20:38:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444270#M34798</guid>
      <dc:creator>ScottMacDonald</dc:creator>
      <dc:date>2013-01-08T20:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Attribute User Input</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444271#M34799</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Oh yes you need to have your whereclause after your typeField parameter assignment.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 Jan 2013 20:46:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444271#M34799</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-01-08T20:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: Select Layer By Attribute User Input</title>
      <link>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444272#M34800</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Perfect - it's working fine now, thanks a lot for your help&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 Jan 2013 15:53:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-layer-by-attribute-user-input/m-p/444272#M34800</guid>
      <dc:creator>ScottMacDonald</dc:creator>
      <dc:date>2013-01-09T15:53:47Z</dc:date>
    </item>
  </channel>
</rss>

