Select to view content in your preferred language

Problem with Update Cursor

846
5
Jump to solution
03-19-2013 02:48 AM
MohanGovindaraj1
Emerging Contributor
In my below script, i am trying to export features which are having null or 0 values for some particular fields(listed in fieldList) from the GDB.I found that the problem is with update cursor.I tring for long time and still not able to solve. Could someone help me how to do it?

import arcpy,os  arcpy.env.overwriteOutput = True  targetGdb = arcpy.GetParameterAsText(0) workFolder = os.path.dirname(targetGdb)  outFolder = workFolder + os.sep + "CheckforErrors" if not arcpy.Exists(outFolder):     arcpy.CreateFolder_management(workFolder, "CheckforErrors")  attrErrPt = "AttributionError_Points" attrErrL = "AttributionError_Lines" attrErrP = "AttributionError_Polygons"  if not arcpy.Exists(attrErrPt):     arcpy.CreateFeatureclass_management(outFolder, attrErrPt, "POINT") if not arcpy.Exists(attrErrL):     arcpy.CreateFeatureclass_management(outFolder, attrErrL, "POLYLINE") if not arcpy.Exists(attrErrP):     arcpy.CreateFeatureclass_management(outFolder, attrErrP, "POLYGON")      outErrPt = outFolder + os.sep + "AttributionError_Points" outErrL = outFolder + os.sep + "AttributionError_Lines" outErrP = outFolder + os.sep + "AttributionError_Polygons"  fieldList = ['feattype','idarpt','name','source','ZV7','featsource','imagery','status','idthr','Z5M',              'idstd','tdze','tdzslope','brngtrue','brngmag','rwyslope','tora','toda','asda','lda','vasis',              'cat','thrtype','pntsttyp','marking','lighting','radius','height','material','idp','idrwy','idlin',              'color','style','direc','linsttyp','catstop','gsurftyp','idapron','idbase','ident','surftype','frq',              'station','idhot','jetway','surftype','width','length','idrwi','featbase','bridge','plysttyp']  arcpy.env.workspace = targetGdb  datasetList = arcpy.ListDatasets('','feature')  for dataset in datasetList:     fcs = arcpy.ListFeatureClasses('','', dataset)          for fc in fcs:         desc = arcpy.Describe(fc)         fcType = desc.ShapeType         arcpy.AddMessage(fc)         arcpy.AddMessage(fcType)         if fcType == "Point":             fields = arcpy.ListFields(fc)             for field in fields:                 fname = field.name                 arcpy.AddMessage(field.name)                 if field.name in fieldList:                     rows = arcpy.SearchCursor(fc)                     newRows = arcpy.InsertCursor(outErrPt)                     row = rows.next()                     while row:                         rVal = row.getValue(fname)                         if rVal == None:                             newRo = newRows.newRow()                             newRo.shape = row.shape                             newRows.insertRow(newRo)                          row = rows.next()                     del row, rows, newRows                                      elif fcType == "Polyline":             fields = arcpy.ListFields(fc)             newRows2 = arcpy.InsertCursor(outErrL)             for field in fields:                 fname = field.name                 arcpy.AddMessage(field.name)                 if field.name in fieldList:                     rows = arcpy.SearchCursor(fc)                     row = rows.next()                     while row:                         rVal = row.getValue(fname)                                                  if rVal == None:                             newRow2 = newRows2.newRow()                             newRow2.shape = row.shape                             newRows2.insertRow(newRow2)                         row = rows.next()                     del row, rows                                     elif fcType == "Polygon":             fields = arcpy.ListFields(fc)             newRows3 = arcpy.InsertCursor(outErrP)             for field in fields:                 fname = field.name                 arcpy.AddMessage(field.name)                 if field.name in fieldList:                     rows = arcpy.SearchCursor(fc)                     row = rows.next()                     while row:                         rVal = row.getValue(fname)                                                  if rVal == None:                             newRow3 = newRows3.newRow()                             newRow3.shape = row.shape                             newRows3.insertRow(newRow3)                                                          row = rows.next()                     del row, rows
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
  But I have 40+ feature classes and i want only three output error files ( point, line and polygon). how can I use copy features tool here, i believe it would overwrite the out file every time or would create many output files.


You can use the append tool.


I want to know what is causing error in my above script, I tested it and the execution stops exactly at the line where lnsertCursor comes.


My guess is that since you created the feature class in a folder, it will have a shp extension. This probably would fix it (line 24):

outErrPt = outFolder + os.sep + "AttributionError_Points" + ".shp"

There's a neat tool in ArcInfo Worsktation that was designed for attribute testing, kind of miss that tool. The currently recommended workflow would be to set up and apply domains in the geodatabase, but that sure seems like a lot of trouble for all your fields.

View solution in original post

0 Kudos
5 Replies
MohanGovindaraj1
Emerging Contributor
Sorry, not update cursor, it is 'Insert Cursor'.
0 Kudos
curtvprice
MVP Alum
I believe it would be a lot less complex to simply create a layer, build a query string off the data using a Search Cursor, and copy the records using the Copy Features tool.
0 Kudos
MohanGovindaraj1
Emerging Contributor
I believe it would be a lot less complex to simply create a layer, build a query string off the data using a Search Cursor, and copy the records using the Copy Features tool.


Thanks for reply,

But I have 40+ feature classes and i want only three output error files ( point, line and polygon). how can I use copy features tool
here, i believe it would overwrite the out file every time or would create many output files.

Also I want to know what is causing error in my above script, I tested it and the execution stops exactly at the line where
lnsertCursor comes.
0 Kudos
curtvprice
MVP Alum
  But I have 40+ feature classes and i want only three output error files ( point, line and polygon). how can I use copy features tool here, i believe it would overwrite the out file every time or would create many output files.


You can use the append tool.


I want to know what is causing error in my above script, I tested it and the execution stops exactly at the line where lnsertCursor comes.


My guess is that since you created the feature class in a folder, it will have a shp extension. This probably would fix it (line 24):

outErrPt = outFolder + os.sep + "AttributionError_Points" + ".shp"

There's a neat tool in ArcInfo Worsktation that was designed for attribute testing, kind of miss that tool. The currently recommended workflow would be to set up and apply domains in the geodatabase, but that sure seems like a lot of trouble for all your fields.
0 Kudos
MohanGovindaraj1
Emerging Contributor
You can use the append tool.


This one i will try...


My guess is that since you created the feature class in a folder, it will have a shp extension. This probably would fix it (line 24):

outErrPt = outFolder + os.sep + "AttributionError_Points" + ".shp"

There's a neat tool in ArcInfo Worsktation that was designed for attribute testing, kind of miss that tool. The currently recommended workflow would be to set up and apply domains in the geodatabase, but that sure seems like a lot of trouble for all your fields.

Thank you very much Price, it has worked fine..
0 Kudos