0

I have a problem from combining two search cursors (one for a table and other for a feature class), there are missed records in the first table, that have not been considered by the first search cursor, I have tried many trials for error handling like join both tables and using try and except syntax, but all of that have not worked, also after reading answers on previous questions here, I have tried (cursor.reset() function), but it gives no end for the script. here you are the part of my code:

cursor3= arcpy.da.SearchCursor(table1, [field1, field2]) 
for row in cursor3:
    try:
        cursor4= arcpy.da.SearchCursor(FeatureClass1, ['OBJECTID'])
        for row1 in cursor4:
            try:
                if row1[0] == row[0] or row1[0] == row[1]:
                    FeatureClass2= arcpy.env.workspace+"\\FeatureClass2"
                    where = "\"%s\" = %i OR \"%s\" = %i" % ('OBJECTID', row[0],'OBJECTID',row[1])
                    arcpy.Select_analysis(FeatureClass1, FeatureClass2, where)

                    # Local variables:
                    FeatureClass2 = "FeatureClass2"
                    FeatureClass2_Intersect = "FeatureClass2_Intersect"
                    # Process: Intersect
                    arcpy.Intersect_analysis(FeatureClass2, FeatureClass2_Intersect, "ALL", "", "LINE")

                    arcpy.AddField_management(FeatureClass2_Intersect,'Value',"DOUBLE")
                    cursor2= arcpy.da.UpdateCursor(FeatureClass2_Intersect, 'Value')
                    for row2 in cursor2:
                        try:
                            if row2[0] ==6:
                                row2= cursor2.next()
                                if row2[0] ==7:
                                    row2[1]=float(42)
                                    cursor2.updateRow(row2)# give a value to value field
                        except:
                            del cursor2


            except:
                row1= cursor4.next()

    except:
        row= cursor3.next()
        del cursor3
2
  • 2
    Can you please edit your question and include a bit of info about what you want your script to do (the expected result) and what errors (if any) or what it is actually outputting instead
    – Midavalo
    Jul 16, 2016 at 21:00
  • 1
    Try/except statements hide errors that would help you when debugging so please remove them then show us any error that results from running the code snippet that you present.
    – PolyGeo
    Jul 16, 2016 at 23:42

1 Answer 1

3

All search cursors must be loaded to dictinaries. This will dramatically improve your performance. I never ever embed cursors now and never will again. The look up of a dictionary is immediate without looping and all you have to do is check for the common key value. All fields outside the key can be loaded into a list (or list of lists) in the dictionary.

See my blog on Geonet on Turbo Charging Data Manipulation with Python Cursors and Dictionaries. It will transform your cursor routines beyond belief. I have also posted this solution numerous times here if you search for dictionaries.

Your script also should fail since the update cursor only can access one field called Value, but you are trying to read and write using two fields. I think you forgot to include the Field that ==6 or ==7 in the update cursor field list since the Value field will alway be Null given that you just created that field.

Your code should also fail the second time the outside cursor loops and you try to create another copy of the Intersect feature class, since scripts cannot overwrite feature classes if they are standalone and otherwise you will be destroying the work of each loop if you do overwrite the Intersect output. In other words it looks like you have not worked through the logic of this script very well.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.