Hello, I am trying to use an update cursor on an attribute table of a feature class (called Lancowatersheds) in order to replace certain characters in values in a specific field of the table (called STORMWATER). I tried the following code and when I used the print function to show desired changes (remove all "-" and "\" from all values in the STORMWATER field and replace with " "), the update cursor did not seem to work (values still had those funky characters in them):
<SPAN class="comment token"># Import arcpy</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token"># Set environments</SPAN>
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:/GIS_Data/Test"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># Input feature class</SPAN>
in_fc <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:/GIS_Data/Test/TEST/Lancowatersheds.shp"</SPAN>
<SPAN class="keyword token">with</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"STORMWATER"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"-"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" "</SPAN><SPAN class="punctuation token">)</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"\\"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" "</SPAN><SPAN class="punctuation token">)</SPAN>
cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> row
<SPAN class="keyword token">del</SPAN> row
<SPAN class="keyword token">del</SPAN> cursor
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Here's a list of all the original values in the STORMWATER field:
['SUSQUEHANNA RIVER', 'PEQUEA CREEK', 'OCTORARO CREEK', 'MILL CREEK', 'LITTLE CONESTOGA CREEK', 'CONOWINGO CREEK', 'CONEWAGO CREEK-DAUPHIN', 'CONESTOGA RIVER', 'COCALICO CREEK', 'CHICKIES CREEK', 'DONEGAL\LOWER CHICKIES CREEK']
I then put all the desired values from the "STORMWATER" field into a list (e.g. there should be no weird characters in the values because I ran the update cursor). I wanted to use this list to select certain features from the original shapefile "Lancowatersheds" and make that selection a standalone feature class. Here's that code:
wsheds <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'SUSQUEHANNA RIVER'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PEQUEA CREEK'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'OCTORARO CREEK'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'MILL CREEK'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'LITTLE CONESTOGA CREEK'</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'CONOWINGO CREEK'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CONEWAGO CREEK DAUPHIN'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CONESTOGA RIVER'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'COCALICO CREEK'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CHICKIES CREEK'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'DONEGAL LOWER CHICKIES CREEK'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> shed <SPAN class="keyword token">in</SPAN> wsheds<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer_management<SPAN class="punctuation token">(</SPAN>in_fc<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"lyr"</SPAN><SPAN class="punctuation token">)</SPAN>
whereExp <SPAN class="operator token">=</SPAN> <SPAN class="string token">"\"STORMWATER\" = \'{0}\'"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>shed<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lyr"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> whereExp<SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CopyFeatures_management<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lyr"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'{0:s}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>shed<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I got the above code to work for all the objects in the my "wsheds" list whose original values did not contain a "-" or "\" , but for the two objects in the STORMWATER field whose original values contained those characters, no such luck. Maybe automating is a bit much for such a small data set, but I guess my ultimate goal is to understand how to use update cursors to replace any character in a value in any field of an attribute table. I could see this being a useful skill when dealing with much larger data sets. Thanks!!