设置值时使用 UpdateCursor 时出现问题
Trouble using UpdateCursor when setting the value
使用 ArcDesktop 10.1 和 Python 2.7:
我正在编写一个代码,该代码在 13 个字段中搜索值,并根据它在这 13 个字段中找到的内容,连接一个字符串并将结果放入现有(空)字段中。
它使用搜索光标来搜索这 13 个字段。然后在更新游标中使用该结果连接字符串。
我无法使用 setValue 将结果输入字段 - 下面代码的第 40 行 @ urow.setValue(commentsField, easementType)。错误消息非常无用(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
没有信息的 999999 错误是最糟糕的错误之一。
我建议对您的方法进行一些修改,以简化故障排除。首先,use the da Cursors——它们速度更快,语法也更简单。
其次,您不需要单独的搜索和更新 -- 除了更新字段外,更新还可以 "search" 同一行中的其他字段。 (当前代码,假设它工作正常,会将相同的 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)
使用 ArcDesktop 10.1 和 Python 2.7: 我正在编写一个代码,该代码在 13 个字段中搜索值,并根据它在这 13 个字段中找到的内容,连接一个字符串并将结果放入现有(空)字段中。
它使用搜索光标来搜索这 13 个字段。然后在更新游标中使用该结果连接字符串。
我无法使用 setValue 将结果输入字段 - 下面代码的第 40 行 @ urow.setValue(commentsField, easementType)。错误消息非常无用(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
没有信息的 999999 错误是最糟糕的错误之一。
我建议对您的方法进行一些修改,以简化故障排除。首先,use the da Cursors——它们速度更快,语法也更简单。
其次,您不需要单独的搜索和更新 -- 除了更新字段外,更新还可以 "search" 同一行中的其他字段。 (当前代码,假设它工作正常,会将相同的 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)