由于 IndexError,游标无法更新字段

Cursor cannot update field because of IndexError

我正在使用 arcpy.da.UpdateCursor 使用列表中的元素更新 UnitName 字段。我的脚本主要基于 .

执行成功:

import arcpy

#fields from the feature class table
field = ['Unit', 'UnitName']

#use a cursor to parse the table of the feature class
with arcpy.da.UpdateCursor(fc, field) as cursor:
    for row in cursor: 
        unit = row[0] #get Unit in field list
        s = str(unit).split() #split strings in Unit field 
        print(s) #print list

以下是打印输出的几个示例:

['200']
['STE', '112']
['STE', 'L']
['UNIT', 'A']
['STE', 'B']
['STE', 'A']
['None']
['None']

但是,当我在打印语句中输入索引号时,我得到了 IndexError: list index out of range。有趣的是我没有立即得到错误。在错误发生之前打印了一堆元素。我认为这是因为 None 元素。

import arcpy

#fields from the feature class table
field = ['Unit', 'UnitName']

#use a cursor to parse the table of the feature class
with arcpy.da.UpdateCursor(fc, field) as cursor:
    for row in cursor: 
        unit = row[1] #get Unit in field list
        s = str(unit).split() #split strings in Unit field 
        print(s[1]) #print element in second position
        row[1] = s[1] #create variable for the element
        cursor.updateRow(row) #use cursor to update "UnitName" field with element in second position
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

我通过访问列表中的最后一项而不是第二项解决了我的问题。

这个:

print(s[-1]) #prints None elements too

不是这个:

print(s[1])