我目前正在尝试填充2个字段。它们都已在我希望用现有要素类中的数据填充的表中创建。这个想法是复制与特定项目#匹配的所需要素类的所有数据。与项目#匹配的行将复制到具有匹配字段的空白模板。到目前为止一切都很好,除了我需要将数据从OBJECT ID字段和要素类的名称推送到表格中的2个字段。
**def featureClassName(table_path):
arcpy.AddMessage("Calculating Feature Class Name...")
print "Calculating Feature Class Name..."
featureClass = "FeatureClass"
SDE_ID = "SDE_ID"
fc_desc = arcpy.Describe(table_path)
lists = arcpy.ListFields(table_path)
print lists
with arcpy.da.SearchCursor(table_path, featureClass = "\"NAME\"" + " Is NULL") as cursor:
for row in cursor:
print row
if row.FEATURECLASS = str.replace(row.FEATURECLASS, "*", fc):
cursor.updateRow(row)
print row
del cursor, row
else:
pass**
上面的代码是我的尝试,其中有许多人使用要素类的名称填充字段。 我已经尝试对OID做同样的事情。
**for fc in fcs:
print fc
if fc:
print "Making Layer..."
lyr = arcpy.MakeFeatureLayer_management (fc, r"in_memory\temp", whereClause)
fcCount = int(arcpy.GetCount_management(lyr).getOutput(0))
print fcCount
if fcCount > 0:
tbl = arcpy.CopyRows_management(lyr, r"in_memory\temp2")
arcpy.AddMessage("Checking for Feature Class Name...")
arcpy.AddMessage("Appending...")
print "Appending..."
arcpy.Append_management(tbl, table_path, "NO_TEST")
print "Checking for Feature Class Name..."
featureClassName(table_path)
del fc, tbl, lyr, fcCount
arcpy.Delete_management(r"in_memory\temp")
arcpy.Delete_management(r"in_memory\temp2")
else:
arcpy.AddMessage("Pass... " + fc)
print ("Pass... " + fc)
del fc, lyr, fcCount
arcpy.Delete_management(r"in_memory\temp")
pass**
此代码是数据集中要素类的主循环,我创建了一个新的图层/表,用于将数据复制到表中。要素类名称和OID的数据没有要推送的数据,所以我被卡住了。
谢谢大家
答案 0 :(得分:2)
你有很多错误。首先,您没有正确设置光标。如果要更新,它必须是updateCursor,顺便说一句,你调用了一个你调用错误的searchCursor。其次,你在行中使用了=(赋值)而不是==(相等比较)行“如果是row.FEATURECLASS ...那么下面的2行,你的缩进就搞砸了几行。而且根本不清楚你的函数知道fc的值。将它作为一个arg传递确定。存在一堆其他问题,但让我们给你一个可行的例子,你可以研究它:
def featureClassName(table_path, fc):
'''Will update the FEATURECLASS FIELD in table_path rows with
value of fc (string) where FEATURECLASS field is currently null '''
arcpy.AddMessage("Calculating Feature Class Name...")
print "Calculating Feature Class Name..."
#delimit field correctly for the query expression
df = arcpy.AddFieldDelimiters(fc, 'FEATURECLASS')
ex = df + " is NULL"
flds = ['FEATURECLASS']
#in case we don't get rows, del will bomb below unless we put in a ref
#to row
row = None
#do the work
with arcpy.da.UpdateCursor(table_path, flds, ex) as cursor:
for row in cursor:
row[0] = fc #or basename, don't know which you want
cursor.updateRow(row)
del cursor, row
请注意,我们现在将fc的名称作为arg传递,因此您必须在其余代码中处理该名称。此外,最好使用AddFieldDelimiter,因为不同的fc需要不同的分隔符,而且文档根本不清楚(有时它们只是错误的)。
祝你好运,迈克