【发布时间】:2020-10-14 20:41:10
【问题描述】:
我正在使用arcpy.da.UpdateCursor 使用列表中的元素更新UnitName 字段。我的脚本主要基于this question。
这会成功执行:
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
【问题讨论】: