AttributeError:'NoneType'对象在游标中没有属性'split'

时间:2017-01-04 20:42:06

标签: python-2.7

我研究了具有相同错误结果的帖子,但是当我的光标在我的表中遇到空值并尝试执行拆分时,我认为我正在发生这种情况。我尝试使用条件语句让程序继续运行,如果它遇到None值但仍然得到相同的错误。这是我的代码:

f1 = "Address"

f2 = "Cust1stStWord"

f3 = "ST_NM_BASE"

f4 = "Street1stStWord"

print "Running cursors"
with arcpy.da.UpdateCursor("gimLyrJoin",[f1, f2, f3, f4]) as cursor:
    for row in cursor:
        if row[2] != None:
            continue
        # first update fields with street names that have directional prefixes
        if len(row[0].split(" ")[1]) == 1 and row[0].split(" ")[2] != "ST":
            row[1] = row[0].split(" ")[2][0:3]
            row[3] = row[2].split(" ")[2][0:3]
        # now look for street names that start with SAINT in customer records and change them to ST
        elif row[0].split(" ")[1] == "SAINT":
            row[1] = "ST "
            row[3] = row[2].split(" ")[1][0:3]
        #Change remaining records to first three letters of street name
        else:
            row[1] = row[0].split(" ")[1][0:3]
            row[3] = row[2].split(" ")[1][0:3]
        cursor.UpdateRow(row)

这是错误:

  Traceback (most recent call last):
  File "N:\Python\Completed scripts\GIM_Street_Updates.py", line 78, in <module>
    row[3] = row[2].split(" ")[1][0:3]
AttributeError: 'NoneType' object has no attribute 'split'

1 个答案:

答案 0 :(得分:0)

在循环开始时,我提出了一个条件,找到row [2] == None,将值更改为&#34; &#34;并在条件结束时输入Updaterow(行)并继续语句,因此split方法不会在循环中稍后抛出错误:

with arcpy.da.UpdateCursor("gimLyrJoin",[f1, f2, f3, f4]) as cursor:
        for row in cursor:
            # fix null values
            if row[2] == None:
                row[2] = " "
                cursor.updateRow(row)
                continue
            elif len(row[0].split(" ")[1]) == 1 and row[0].split(" ")[2] != "ST":
                row[1] = row[0].split(" ")[2][0:3]
                # ST_NM_BASE seems to not include prefix so okay to 0 index
                row[3] = row[2].split(" ")[0][0:3]
            # now look for street names that start with SAINT in customer records and change them to ST
            elif row[0].split(" ")[1] == "SAINT":
                row[1] = "ST "
                row[3] = row[2].split(" ")[0][0:3]
            #Change remaining records to first three letters of street name
            else:
                row[1] = row[0].split(" ")[1][0:3]
                row[3] = row[2].split(" ")[0][0:3]
            cursor.updateRow(row)
相关问题