<?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: error in SelectLayerByAttribute in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523275#M41025</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There is a button on the right side (sign in first) :D.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 09 May 2012 07:41:00 GMT</pubDate>
    <dc:creator>FabianBlau</dc:creator>
    <dc:date>2012-05-09T07:41:00Z</dc:date>
    <item>
      <title>error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523268#M41018</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;System: ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;pythonWin: 2.5&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Problem 1:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Using SelectLayerByAttribute but error showed:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 000358: Invalid expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly help indicate the mistake in the query code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, a shape file is attached for trial.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks a lot. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt; ##Script Name: Delete rows ##Description: delete selected rows ##Created By: Elaine Kuo ##Date: 04/05/2012&amp;nbsp; #Import standard library modules import arcgisscripting import os&amp;nbsp; #Create the Geoprocessor object gp = arcgisscripting.create(9.3)&amp;nbsp; #Set the input workspace #GP.workspace = sys.argv[1] #Set the workspace. gp.Workspace= "G:/temp"&amp;nbsp; #Set the output workspace #outWorkspace = sys.argv[2] #Set the workspace. List all of the feature classes in the dataset outWorkspace= "G:/temp/test"&amp;nbsp; #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()&amp;nbsp; # Loop through every item in the list that was just generated for fc in fcs:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object. &amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc) &amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Make featureclasses as layers &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.OverWriteOutput = 1 &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management" &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer("geor0313_p.shp","geor0313_p_lyr") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #Get a list of the fields in the featureclass &amp;nbsp;&amp;nbsp;&amp;nbsp; fields = gp.ListFields("geor0313_p_lyr", "C*", "String")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select records to be deleted (C*, i.e. C7658) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = "\"C*\" = 'R'" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute("geor0313_p_lyr", "ADD_TO_SELECTION", query)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Delete selected records &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.deleterows("geor0313_p_lyr") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #Validate the new feature class name for the output workspace. &amp;nbsp;&amp;nbsp;&amp;nbsp; outFeatureClass = outWorkspace + os.sep +GP.ValidateTableName("geor0313_p_lyr",outWorkspace)&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 May 2012 22:00:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523268#M41018</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-05-05T22:00:35Z</dc:date>
    </item>
    <item>
      <title>Re: error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523269#M41019</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The where clause in selectlayerbyattribute does not recognize wildcard characters. You need to use the field variable that you're creating in the inner loop instead. Try this:&lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;query = "\"%s\" = 'R'" % field&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;This will insert the field variable into the query expression each time the loop executes.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 06 May 2012 13:03:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523269#M41019</guid>
      <dc:creator>BruceNielsen</dc:creator>
      <dc:date>2012-05-06T13:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523270#M41020</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;system arcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the response.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However, the same error still occurred after modifying the code as below.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly help and thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;##Script Name: Delete rows
##Description: delete selected rows
##Created By: Elaine Kuo
##Date: 04/05/2012

#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

#Set the input workspace
#GP.workspace = sys.argv[1]
#Set the workspace.
gp.Workspace= "G:/temp"

#Set the output workspace
#outWorkspace = sys.argv[2]
#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "G:/temp/test"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

# Loop through every item in the list that was just generated
for fc in fcs:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object.
&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make featureclasses as layers
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.OverWriteOutput = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management"
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.MakeFeatureLayer("geor0313_p.shp","geor0313_p_lyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Get a list of the fields in the featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp; fields = gp.ListFields("geor0313_p_lyr", "C*", "String")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Select records to be deleted (C*, i.e. C7658)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; query = "\"%s\" = 'R'" % field
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.SelectLayerByAttribute("geor0313_p_lyr", "ADD_TO_SELECTION", query)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Delete selected records
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.deleterows("geor0313_p_lyr")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Validate the new feature class name for the output workspace.
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFeatureClass = outWorkspace + os.sep +GP.ValidateTableName("geor0313_p_lyr",outWorkspace)


&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:48:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523270#M41020</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2021-12-11T22:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523271#M41021</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I guess field is a Field Object, not a String. Check this with gp.addMessage(type(field)).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;query = "\"%s\" = 'R'" % field.Name&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 May 2012 11:14:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523271#M41021</guid>
      <dc:creator>FabianBlau</dc:creator>
      <dc:date>2012-05-07T11:14:41Z</dc:date>
    </item>
    <item>
      <title>Re: error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523272#M41022</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks a lot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You are right.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly explain why it works with "query = "\"%s\" = 'R'" % field.Name" instead of "query = "\"%s\" = 'R'" % field"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, please kindly help how to run gp.addMessage(type(field)) using PythonWin for a beginner. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 07 May 2012 22:45:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523272#M41022</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-05-07T22:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523273#M41023</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In arcgisscripting 9.3 ListFeatureClasses() returns not a string but an object. &lt;/SPAN&gt;&lt;BR /&gt;&lt;A class="jive-link-external-small" href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=ListFields_method" rel="nofollow" target="_blank"&gt;http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=ListFields_method&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This object is like a container for severals varaibles and functions (for a&amp;nbsp; detailed explanation of objects read some smart books;) &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://en.wikipedia.org/wiki/Object_%28computer_science%29" rel="nofollow" target="_blank"&gt;http://en.wikipedia.org/wiki/Object_%28computer_science%29&lt;/A&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;One of the attributes is the fieldname -&amp;gt; &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Field_properties" rel="nofollow" target="_blank"&gt;http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Field_properties&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Use AddMessage when you start a script from a toolbox.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Use print starting a script in python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;print type(field)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 May 2012 12:22:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523273#M41023</guid>
      <dc:creator>FabianBlau</dc:creator>
      <dc:date>2012-05-08T12:22:23Z</dc:date>
    </item>
    <item>
      <title>Re: error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523274#M41024</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks a lot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How to mark that you helped answer my questions &lt;span class="lia-unicode-emoji" title=":face_with_open_mouth:"&gt;😮&lt;/span&gt;&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 08 May 2012 21:36:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523274#M41024</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-05-08T21:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: error in SelectLayerByAttribute</title>
      <link>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523275#M41025</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;There is a button on the right side (sign in first) :D.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 09 May 2012 07:41:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-in-selectlayerbyattribute/m-p/523275#M41025</guid>
      <dc:creator>FabianBlau</dc:creator>
      <dc:date>2012-05-09T07:41:00Z</dc:date>
    </item>
  </channel>
</rss>

