使用ArcDesktop 10.1& Python 2.7: 我正在研究一个代码,该代码在13个字段中搜索值,并根据它在这13个字段中找到的内容,连接字符串并将结果放入现有(空)字段。
它使用搜索光标搜索13个字段。然后在更新游标中使用它的结果来连接字符串。
我无法使用setValue - @ urow.setValue(commentsField,easementType)下面代码的第40行将结果输入字段。错误消息非常无用(RuntimeError:ERROR 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 :(得分: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)