I'm using arcpy.da.UpdateCursor and split() to update the unit name field of my table with an element from a list.
This executes successfully:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token">#fields from the feature class table</SPAN>
field <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Unit'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'UnitName'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#use a cursor to parse the table of the feature class</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>fc<SPAN class="punctuation token">,</SPAN> field<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>
unit <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#get Unit in field list</SPAN>
s <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>unit<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#split strings in Unit field </SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>s<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#print list</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>Here are a few examples of what prints out:
['200']
['STE', '112']
['STE', 'L']
['UNIT', 'A']
['STE', 'B']
['STE', 'A']
['None']
['None']<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
But, when I put an index number on the print statement I get the IndexError: list index out of range. Funny thing is I don't get the error right away. A bunch of elements are printed before the error occurs. I think it is because of the None elements.
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="comment token">#fields from the feature class table</SPAN>
field <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Unit'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'UnitName'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#use a cursor to parse the table of the feature class</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>fc<SPAN class="punctuation token">,</SPAN> field<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>
unit <SPAN class="operator token">=</SPAN> row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#get Unit in field list</SPAN>
s <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>unit<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#split strings in Unit field </SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>s<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#print element in second position</SPAN>
row<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> s<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token">#create variable for the element</SPAN>
cursor<SPAN class="punctuation token">.</SPAN>updateRow<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#use cursor to update "UnitName" field with element in second position</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>Prints:
200
112
L
A
B
A
Traceback (most recent call last):
File "\GISstaff\Jared\Python Scripts\ArcGISPro\USPS_ADDRE_copy.py", line 40, in <module>
unitname()
File "\GISstaff\Jared\Python Scripts\ArcGISPro\USPS_ADDRE_copy.py", line 34, in unitname
print(s[1])
IndexError: list index out of range<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>