<?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: Python - Remove rows in Shapefile Based on Criteria in a Field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119112#M9382</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That was what I needed, thank you.&amp;nbsp; And that's where the Syntax highlighter is, I had thought it&amp;nbsp;was called something different in the past and that's why...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 15 Mar 2018 16:25:10 GMT</pubDate>
    <dc:creator>PeteJordan</dc:creator>
    <dc:date>2018-03-15T16:25:10Z</dc:date>
    <item>
      <title>Python - Remove rows in Shapefile Based on Criteria in a Field</title>
      <link>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119107#M9377</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;nbsp; I have a sample script for a test shapefile &lt;EM&gt;(which I eventually will apply to an actual shapefile, but for simplicity I'll just use this)&amp;nbsp;&lt;/EM&gt;that I want to run outside of Arc as a standalone script.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Shape file name&lt;/STRONG&gt;: TEST.shp&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Field:&lt;/STRONG&gt; fclass&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Attributes to Purge (in fclass):&lt;/STRONG&gt; "A", "B", "C"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; So with this script, I want all records that have an fclass containing A, B or C to be deleted and leaving everything else.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here's where I have an issue.&amp;nbsp; I can get this script to work with just one of the attributes, but not a combination of attributes &lt;EM&gt;(Sorry I can't seem to find a script code option to format this better)&lt;/EM&gt;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;print "Importing..."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;import arcpy, os&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;shp = r'U:\TEST\TEST.shp'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;list = ['1', '2', '3']&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;print "Purging unnecessary fclass values..."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;with arcpy.da.UpdateCursor(shp, "fclass") as cursor:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt; for row in cursor:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px;"&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &lt;SPAN style="color: #ff00ff;"&gt;&amp;nbsp;#if row[0] == "1": -- if I use just this, it works and deletes all rows with "1"&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;if row[0] == &lt;UL&gt;:&lt;/UL&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;cursor.deleteRow()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-size: 13px; color: #0000ff;"&gt;print "Completed"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Using the&amp;nbsp;&lt;SPAN style="color: #0000ff;"&gt;&lt;EM&gt;if row[0] == &lt;UL&gt;:&amp;nbsp;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;SPAN&gt;doesn't do anything and neither does&amp;nbsp;&lt;EM style="color: #0000ff;"&gt;if row[0] == ["1","2","3"]&lt;/EM&gt;&lt;SPAN style="color: #0000ff;"&gt;:&amp;nbsp;&lt;SPAN style="color: #000000;"&gt;I also don't want to&amp;nbsp;use multiple OR&amp;nbsp;statements if I can help it, similar to this &lt;EM&gt;(though the syntax for python OR statements might not look like this)&lt;/EM&gt;:&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/UL&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;&amp;nbsp; &amp;nbsp;if row[0] == '1': OR&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;&amp;nbsp; &amp;nbsp;if row[0] == '2': OR&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;&amp;nbsp; &amp;nbsp;if row[0] == '3':&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;SPAN style="color: #000000;"&gt;&amp;nbsp; I've also played around using the different quotes as well in the script switching between the &lt;STRONG&gt;"&lt;/STRONG&gt; and the &lt;STRONG&gt;'&lt;/STRONG&gt; without any success as well, thinking that was maybe the issue.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;SPAN style="color: #000000;"&gt;&amp;nbsp; I am not getting any errors at all, but it's just not deleting the records at all either...&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;SPAN style="color: #000000;"&gt;Thanks&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #3d3d3d;"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2018 15:06:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119107#M9377</guid>
      <dc:creator>PeteJordan</dc:creator>
      <dc:date>2018-03-15T15:06:32Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Remove rows in Shapefile Based on Criteria in a Field</title>
      <link>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119108#M9378</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The Python syntax you are looking for is&lt;SPAN style="font-family: courier\ new, courier, monospace;"&gt; item in list&lt;/SPAN&gt;.&amp;nbsp; That said, a more database/set-based approach would be to &lt;A href="http://pro.arcgis.com/en/pro-app/tool-reference/data-management/make-feature-layer.htm"&gt;Make Feature Layer&lt;/A&gt;, &lt;A href="http://pro.arcgis.com/en/pro-app/tool-reference/data-management/select-layer-by-attribute.htm"&gt;Select Layer By Attribute&lt;/A&gt;, and then &lt;A href="http://pro.arcgis.com/en/pro-app/tool-reference/data-management/delete-features.htm"&gt;Delete Features&lt;/A&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2018 15:20:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119108#M9378</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2018-03-15T15:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Remove rows in Shapefile Based on Criteria in a Field</title>
      <link>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119109#M9379</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You should use the in python function to see if the value of the row you're iterating over is found in the list. &amp;nbsp;See below for an example. &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Note: &amp;nbsp;make sure to convert the values to a string before comparing (row 10), since the values in the deletedValues list are strings.&amp;nbsp;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;CODE&gt;&lt;SPAN class="keyword token"&gt;import&lt;/SPAN&gt; arcpy

dataset &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; r&lt;SPAN class="string token"&gt;'path\to\dataset.shp'&lt;/SPAN&gt;

deleteValues &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'1'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'2'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'3'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;

cursor &lt;SPAN class="operator token"&gt;=&lt;/SPAN&gt; arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;da&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;UpdateCursor&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;dataset&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;'FIELD'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;

&lt;SPAN class="keyword token"&gt;for&lt;/SPAN&gt; count&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; d &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; enumerate&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;cursor&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="keyword token"&gt;if&lt;/SPAN&gt; str&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;d&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="number token"&gt;0&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt; &lt;SPAN class="keyword token"&gt;in&lt;/SPAN&gt; deleteValues&lt;SPAN class="punctuation token"&gt;:&lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;deleteRow&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
&lt;SPAN class="keyword token"&gt;del&lt;/SPAN&gt; cursor


&lt;SPAN class="keyword token"&gt;print&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;'{0} rows were deleted.'&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;format&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;count&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:57:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119109#M9379</guid>
      <dc:creator>MitchHolley1</dc:creator>
      <dc:date>2021-12-11T06:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Remove rows in Shapefile Based on Criteria in a Field</title>
      <link>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119110#M9380</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;nbsp;I get a &lt;STRONG&gt;"Cursor" not defined&lt;/STRONG&gt; error with the &lt;SPAN style="color: #0000ff;"&gt;cursor.deleteRow()&lt;SPAN style="color: #000000;"&gt;, plus how do you convert the values to a string in row 10?&amp;nbsp; I'm not really familiar with Python.&amp;nbsp; Also what's the format code to make the script show up with the row numbers like you had?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;import arcpy&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;dataset = r'U:\TEST\TEST.shp'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;deleteValues = ['1','2','3']&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;deleteCursor = arcpy.da.UpdateCursor(dataset, ['fclass'])&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;for count, d in enumerate(deleteCursor):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; if str(d[0]) in deleteValues:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cursor.deleteRow()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;del cursor&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #0000ff; font-size: 12px;"&gt;print '{0} rows were deleted.'.format(count)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 15px;"&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-size: 15px;"&gt;Also will look at the &lt;STRONG&gt;Items in list&lt;/STRONG&gt; as well, but I had issues with that yesterday trying to get it to work properly with the examples I found online...&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2018 15:39:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119110#M9380</guid>
      <dc:creator>PeteJordan</dc:creator>
      <dc:date>2018-03-15T15:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Remove rows in Shapefile Based on Criteria in a Field</title>
      <link>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119111#M9381</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;First - I had a syntax error in my original reply. &amp;nbsp;It has been edited, please try that one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Second - Click the three eellipses when writing the response -&amp;gt; click 'More' -&amp;gt; 'Syntax Highlighter'&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2018 15:54:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119111#M9381</guid>
      <dc:creator>MitchHolley1</dc:creator>
      <dc:date>2018-03-15T15:54:26Z</dc:date>
    </item>
    <item>
      <title>Re: Python - Remove rows in Shapefile Based on Criteria in a Field</title>
      <link>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119112#M9382</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That was what I needed, thank you.&amp;nbsp; And that's where the Syntax highlighter is, I had thought it&amp;nbsp;was called something different in the past and that's why...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Mar 2018 16:25:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-remove-rows-in-shapefile-based-on-criteria/m-p/119112#M9382</guid>
      <dc:creator>PeteJordan</dc:creator>
      <dc:date>2018-03-15T16:25:10Z</dc:date>
    </item>
  </channel>
</rss>

