【问题标题】:Cursor cannot update field because of IndexError由于 IndexError,光标无法更新字段
【发布时间】: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

【问题讨论】:

    标签: python list split arcpy


    【解决方案1】:

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

    这个:

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

    不是这个:

    print(s[1])

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-06
      • 2019-10-02
      • 1970-01-01
      • 2019-10-03
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多