2016-04-18 66 views
0

使用ArcDesktop 10.1 & Python 2.7: 我正在研究一個代碼,該代碼搜索13個字段中的值,並根據它在這13個字段中找到的內容連接字符串並將導致現有的(空)字段。設置值時使用UpdateCursor時出現問題

它使用搜索光標搜索13個字段。然後使用更新遊標中的結果連接字符串。

我無法使用setValue - 下面的代碼的第40行獲取結果到字段中@ urow.setValue(commentsField,easementType)。錯誤消息是非常無益的(RuntimeError:錯誤999999:執行函數時出錯。)

我不知道如何正確獲取所需字段中設置的值。任何幫助將不勝感激!

import arcpy, os, math 
from itertools import izip 
arcpy.env.workspace = "C:\\Users\\mdelgado\\Desktop\\WorkinDog.gdb" 

#These are my variables 
fc = "EASEMENTS" 
commentsField = "Comments" 
typeFields = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMM", "DRAIN", "ELEC", "GEN_UTIL", "LANDSCAPE", "PARKING", "PIPELINE", "SAN_SEWR", "SIDEWALK", "SPECIAL", "STM_SEWR", "WATER"] 
fieldNames = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMMUNICATION", "DRAINAGE", "ELECTRIC", "GENERAL UTILITY", "LANDSCAPE", "PARKING", "PIPELINE", "SANITATION SEWER", "SIDEWALK", "SPECIAL", "STORM SEWER", "WATER"] 
fieldValues = [] 
easementType = "" 

#This is my search cursor 
scursor = arcpy.SearchCursor(fc) 
srow = scursor.next() 
for field in typeFields: 
    srowValue = (srow.getValue(field)) 
    fieldValues.append(srowValue) 
    srow = scursor.next() 
print fieldValues 

#This is my update cursor 
ucursor = arcpy.UpdateCursor(fc) 
for urow in ucursor: 

    #This is where I begin the loop to concatenate the comment field 
    for (value, name) in izip(fieldValues, fieldNames): 
     print str(value) + " " + name 

     #This is where I check each field to find out which types the easement is 
     if value == 1: 
      easementType = easementType + name + ", " 

    #This is where I format the final concatenated string 
    easementType = easementType[:-2] 
    print easementType 

    #This is where the field is updated with the final string using the cursor 
    urow.setValue(commentsField, easementType) 
    ucursor.updateRow(urow) 
    urow = cursor.next() 

del urow 
del ucursor 
del srow 
del scursor 
+0

是否正確地執行了一些行再破?你是否檢查過'Comments'字段的長度是否足以容納儘可能長的concateated'easementType'? – Erica

+1

很棒的建議!當看着這個時,我發現我引用了一個字段別名而不是實際的字段名稱。這解決了一個問題。 –

回答

0

無形的999999錯誤是最糟糕的錯誤之一。

我建議對您的方法進行一些修改,以便排除故障。首先,use the da Cursors - 它們更快,語法更簡單一些。

其次,您不需要單獨的搜索和更新 - 除了更新字段之外,更新還可以「搜索」同一行中的其他字段。 (當前的代碼,假設它正常工作,就會把相同fieldValues到每一行的UpdateCursor影響。)

fieldNames = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMMUNICATION", "DRAINAGE", 
       "ELECTRIC", "GENERAL UTILITY", "LANDSCAPE", "PARKING", "PIPELINE", "SANITATION SEWER", 
       "SIDEWALK", "SPECIAL", "STORM SEWER", "WATER"] 
cursorFields = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMM", "DRAIN", 
       "ELEC", "GEN_UTIL", "LANDSCAPE", "PARKING", "PIPELINE", "SAN_SEWR", 
       "SIDEWALK", "SPECIAL", "STM_SEWR", "WATER", "Comments"] 

with arcpy.da.UpdateCursor(fc, cursorFields) as cursor: 
    for row in cursor: 
     easementType = "" 
     for x in range(13): 
      if row[x] == 1: 
       easementType += fieldNames[x] + ", " 
     easementType = easementType[:-2] 
     print easementType 

     row[13] = easementType 
     cursor.updateRow(row) 
+0

這些修改非常棒!他們解決了我所有的問題。腳本現在運行完美。我無法對你的建議表示感謝! –

+0

不客氣:) – Erica