How can I iterate trough a cursor an compare the last row with the actual?
def disolveOemDolungen(bnum):
unterlieger = None
pCursor = arcpy.UpdateCursor(oemFC, "BNUM = '" + bnum + "'","","von; bis; klasse, key","von A; bis A")
row = pCursor.next()
while row:
if not unterlieger is None:
if (unterlieger.getValue("klasse") == 7):
arcpy.AddMessage("Oberlieger:" + unterlieger.getValue("key"))
if (math.fabs(unterlieger.getValue("bis") - row.getValue("von")) <= 1):
unterlieger.setValue("klasse", row.getValue("klasse"))
unterlieger.setValue("sbreite", row.getValue("sbreite"))
pCursor.updateRow(unterlieger)
arcpy.AddMessage("Dissolve mit Oberlieger: " + unterlieger.getValue("key"))
if row.getValue("klasse") == 7:
if not unterlieger is None:
arcpy.AddMessage("Unterlieger " + row.getValue("key"))
if (math.fabs(unterlieger.getValue("bis") - row.getValue("von")) <= 1):
row.setValue("klasse", unterlieger.getValue("klasse"))
row.setValue("sbreite", unterlieger.getValue("sbreite"))
pCursor.updateRow(row)
arcpy.AddMessage("Dissolve mit Unterlieger: " + row.getValue("key"))
unterlieger = row
row = pCursor.next()
Row and unterlieger contains always the same Object. Therefore I can't compare the rows. How can this be done with Python?
Solved! Go to Solution.
It is because of the type of row the arcpy.UpdateCursor is returning. Have you looked into the data access module?
The da module offers better performance as well as a more pythonic experience. For example it is returning the rows as dictionaries.
So this would work:
>>> with arcpy.da.UpdateCursor("mydata", "*") as cursor: ... old = None ... for row in cursor: ... if old is not None: ... print row[0], old[0] ... old = row
Are you wanting to compare attributes from row to row? Read a row into a list or dictionary then when you get to the next row compare and repeat.
In Some cases I have to store values in the "last" Row. So the Values in a List are not enough. I need the Row object.
Have you looked at make feature layer as an option to hold the single row? You could hold it row by row or multiple rows if you needed.
Could you give me an example how I can get hold of multiple rows with make fature layer?
import arcpy fc = "Your Feature Class" desc = arcpy.Describe(fc) oidfld = desc.OIDFieldName fld = arcpy.AddFieldDelimiters(fc,oidfld) print oidfld,fld with arcpy.da.SearchCursor(fc, ("OID@")) as cursor: for row in cursor: tmplyr = "tmplyr"+str(row[0]) #You could create a list of features here to input below query = fld + " in (" + str(row[0]) + ")" arcpy.MakeFeatureLayer_management(fc,tmplyr,query)
It is because of the type of row the arcpy.UpdateCursor is returning. Have you looked into the data access module?
The da module offers better performance as well as a more pythonic experience. For example it is returning the rows as dictionaries.
So this would work:
>>> with arcpy.da.UpdateCursor("mydata", "*") as cursor: ... old = None ... for row in cursor: ... if old is not None: ... print row[0], old[0] ... old = row
Create! That's exactly what I was lookinf for. Thanks a lot!
Not exactly what you need, but I've used this to look for missing and/or duplicate values in the field [recnum], and if there are blanks or duplicates, replace with the next sequential number.
Like I said, not exactly what you are after, but I had this and thought you might get some ideas from it.
R_
def FixDuplicates(): global rec rec = 0 maxValue = arcpy.SearchCursor(infc, "", "", "", "recnum D").next().getValue(myField) #Get 1st row in descending cursor sort if maxValue == None: maxValue = 0 newValue = maxValue + 1 uCur = arcpy.UpdateCursor(infc, "", "", "", "recnum A") for row in uCur: if row.getValue('recnum') and row.getValue('recnum') != 0: curRec = row.getValue('recnum') #First if checks #if the row's rec is equal to the previous row if curRec != rec: rec = row.getValue('recnum') #If its rec is not #equal to the previous row then we are ready to update the existing row else: rec = row.getValue('recnum') row.recnum = newValue newValue += 1 uCur.updateRow(row) else: rec = row.getValue('recnum') row.recnum = newValue newValue += 1 uCur.updateRow(row) del uCur try: infc = r'Database Connections\s14-arcgis-p_OS.sde\PublicWorks.DBO.Sewer_Network\PublicWorks.DBO.ssGravityMain' FixDuplicates()