使用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
是否正確地執行了一些行再破?你是否檢查過'Comments'字段的長度是否足以容納儘可能長的concateated'easementType'? – Erica
很棒的建議!當看着這個時,我發現我引用了一個字段別名而不是實際的字段名稱。這解決了一個問題。 –