I am trying to use arcpy.da.UpdateCursor to find and replace old values in a field with new values. I have made it work but I will ended up with an ridiculously lard if\elif statement (notice below that I stopped after the first 2 items in the list). I would like to avoid this by iterating through both list to update the field. I have tried without any luck. Any suggestions? Code is attached.
How about setting a counter variable?
counter <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> LCStatus<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># print error message and keep original value</SPAN> <SPAN class="comment token">#print "{} not in list".format(row[0])</SPAN> counter <SPAN class="operator token">+=</SPAN><SPAN class="number token">1</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># update value</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> LCStatus<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> cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'{} rows not in list...'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>counter<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></SPAN></SPAN>
The issue i'm running into is that I don't want it to print 100 times "not in list." I just want it to total up the number of times other values are found.
I notice in your second code posting, you are running multiple update cursor loops, once for each item in your list (line 12). With either the indexing solution from Joshua or the dictionary solution, only a single pass with an update cursor is needed and is more efficient.
For the dictionary approach, a check would be something like:
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">in</SPAN> LCStatus<SPAN class="punctuation token">.</SPAN>keys<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># print error message and keep original value</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"{} not in list"</SPAN><SPAN class="punctuation token">.</SPAN>format<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> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># update value</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> LCStatus<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> cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<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 class="comment token">#logic:</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">in</SPAN> listname<SPAN class="punctuation token">:</SPAN> do something <SPAN class="keyword token">elif</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">in</SPAN> listname2<SPAN class="punctuation token">:</SPAN> do something <SPAN class="keyword token">else</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">'Not in either list...'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Thanks for all the responses! Right after I posted this I figured it out how to do it this way. Any suggestions on adding an elif statement that will tell the user if there are any values in the field that are not in the list? Example: "ANB" instead of "ABN"
<SPAN class="keyword token">import</SPAN> arcpy <SPAN class="comment token"># My Feature Class that I want to use</SPAN> fc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># My List of old and new values</SPAN> LifeCycleStatus1 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"ABN"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ACT"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"AB"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"CHGORD"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"DEM"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"INA"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"OTH"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"PRP"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"RMV"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"UNK"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"FV"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"FD"</SPAN><SPAN class="punctuation token">]</SPAN> LifeCycleStatus2 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Abandoned"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Active"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"As Built"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Change Order"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Demolished"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Inactive"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Other"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Proposed"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Removed"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Unknown"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Field Verification Required"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Future Development"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> i<SPAN class="operator token">=</SPAN><SPAN class="number token">0</SPAN> <SPAN class="keyword token">for</SPAN> life <SPAN class="keyword token">in</SPAN> LifeCycleStatus1<SPAN class="punctuation token">:</SPAN> cursor <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"LIFECYCLESTATUS"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> cursor<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> LifeCycleStatus1<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation 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="operator token">=</SPAN> LifeCycleStatus2<SPAN class="punctuation token">[</SPAN>i<SPAN class="punctuation token">]</SPAN> cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> i<SPAN class="operator token">+=</SPAN><SPAN class="number token">1</SPAN> <SPAN class="keyword token">except</SPAN> RuntimeError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">pass</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
To use a dictionary instead of a list (assuming the mapping between your two lists), something like this should also work:
<SPAN class="keyword token">import</SPAN> arcpy fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'G:\pworks\assetmanagement\Local Gov Info Model\Wastewater\Inputs\Valve_Schema.gdb\SControlValve'</SPAN> LCStatus <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="string token">'ABN'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Abandoned'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ACT'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Active'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'AB'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'As Built'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'CHGORD'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Change Order'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'DEM'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Demolished'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'INA'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Inactive'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'OTH'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Other'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PRP'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Proposed'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'RMV'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Removed'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'UNK'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Unknown'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'FV'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Field Verification Required'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'FD'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Future Development'</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> cursor <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"LIFECYCLESTATUS"</SPAN><SPAN class="punctuation token">]</SPAN><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="operator token">=</SPAN> LCStatus<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> cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">del</SPAN> row <SPAN class="keyword token">del</SPAN> cursor <SPAN class="keyword token">except</SPAN> RuntimeError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">pass</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></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>
Click on the three dots above:
Click on the resulting More button and choose Syntax highlighter. Pick the language of choice, and paste your code in the window. Click Ok button at the bottom of the window...
Vince Angelo thanks for the heads up! How do I embed the code like Joshua Bixby did?
Please remember to post code as ASCII text (formatted appropriately), so that those who would help you don't need to retype your code.
- V
Assuming the mapping between the two life cycle status lists is one to one based on position in the list, the following will work:
<SPAN class="keyword token">import</SPAN> arcpy fc <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'G:\pworks\assetmanagement\Local Gov Info Model\Wastewater\Inputs\Valve_Schema.gdb\SControlValve'</SPAN> LifeCycleStatus1 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"ABN"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ACT"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"AB"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"CHGORD"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"DEM"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"INA"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"OTH"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"PRP"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"RMV"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"UNK"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"FV"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"FD"</SPAN><SPAN class="punctuation token">]</SPAN> LifeCycleStatus2 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Abandoned"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Active"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"As Built"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Change Order"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Demolished"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Inactive"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Other"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Proposed"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Removed"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Unknown"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Field Verification Required"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Future Development"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN> cursor <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>UpdateCursor<SPAN class="punctuation token">(</SPAN>fc<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"LIFECYCLESTATUS"</SPAN><SPAN class="punctuation token">]</SPAN><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="operator token">=</SPAN> LifeCycleStatus2<SPAN class="punctuation token">[</SPAN>LifeCycleStatus1<SPAN class="punctuation token">.</SPAN>index<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><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">del</SPAN> row <SPAN class="keyword token">del</SPAN> cursor <SPAN class="keyword token">except</SPAN> RuntimeError<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">pass</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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.