Select to view content in your preferred language

Search & Update Cursor

2682
9
02-17-2020 10:23 PM
MuhammadAttique
Regular Contributor

Hi ,

I am stuck with SearchCursor and Update Cursor in my code, what's wrong with it , 

the scienrio is . I have a table in which "source" , "sink" and "duplicate" fields available.

  1. Search 1 in field "duplicate"  , read row, save "sink" / "source" to variables.
  2. now initiate update cursor , row where source = sink do update .

the output in not the required one.

even I try with one Updatecursor satify the value and update rows , 

the same of the code is as :

with arcpy.da.SearchCursor(SLDTable,SLDfields) as SC01:
      for SC01R in SC01:
            if SC01R[10]==1:
                  tsource = SC01R[2]
                  tsink = SC01R[3]
                  print SC01R
                  with arcpy.da.UpdateCursor(SLDTable,SLDfields) as updC3:
                        for updC3R in updC3:
                              if updC3R[2]==SC01R[3]:
                                 print updC3R
                                 tsource = updC3R[2]
                                 tsink = updC3R[3]
                                 updC3R[8] = sldpt + 1
                                 updC3R[9] = 'BL1'
                                 updC3R[13] = 'Yes'
                                 sldpt += 1
                                 updC3.updateRow(updC3R)
                                 updC3.reset()

0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

/blogs/dan_patterson/2016/08/14/script-formatting 

to provide line numbers for referencing issues.

Also, your if statements have no else, so if a condition fails, your code simply bypasses the section so no useful information is returned to see if the code works.  

Nesting cursors should be avoided.  Use the searchcursor to collect information, then use the update cursor to process the collection.

Your updaterow and reset are probably never reached and/or are at the wrong indentation level

Depending on your Python IDE, check to see if it has the ability to run "code analysis" (eg static code analysis in Spyder).  A lot of issues can be caught and addressed when code doesn't behave as expected.

0 Kudos
MuhammadAttique
Regular Contributor

Hi Dear Dan Patterson,

Thanks for replying , the code was properly indented as I am using pyscripter, here advance option is not work with me , I dont know, why it was not working , any how what I assume , search cursor find specfic value at line 91 then set values as line 92 & 93 , now with reference of that values it should move to line 94 for update curosr. if record statisfy the saved values it update , and retun to line 90 for search next value and so on.

I am trying to avoid nested cursor , how can be avoid , little confusion. 

code example

thanks again for your precious time !!! 

0 Kudos
DanPatterson_Retired
MVP Emeritus
sldpt ... is it initialized outside your loop?

You increment it towards the bottom but it has no initial value

Get rid on the searchcursor and replace it with a SelectLayerByAttributes

arcpy.SelectLayerByAttribute_management("your_featureclass", 'NEW_SELECTION', '"Field_10" == 1')

Since it appears the searchcursor is only looking for values of 1 in what I assume is your duplicates field (SCO1R (?)).

Once the selection is made, only the selected records will be processed.

Are you then are checking to see if field2 ==field3 (source and sink)

and if so, are you then switching the source and sink and updating fields 8, 9 and 13 with new values, and incrementing the sldpt by 1.

Not sure what preceeds this, but applying a selection and working with it will get rid of one cursor.

0 Kudos
MuhammadAttique
Regular Contributor

Thanks Dan Patterson ,

it's really a good idea to apply select by attribute , let me try , and will discuss soon.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Muhammad,

I think you can simplify this with the following:

with arcpy.da.UpdateCursor(SLDTable,SLDfields) as updC3:
     for updC3R in updC3:
         if updC3R[2]==updC3R[3]:
            print updC3R
            updC3R[8] = sldpt + 1
            updC3R[9] = 'BL1'
            updC3R[13] = 'Yes'
            sldpt += 1
            updC3.updateRow(updC3R)
del updC3
0 Kudos
MuhammadAttique
Regular Contributor

Hi Dear Jake Sjunner, 

Thanks for replying , actually I have to search values and then want to update. the advance option is not working with me , that's why code shows like un indented.

0 Kudos
DavidPike
MVP Frequent Contributor

Why are tsource and tsink on the left of the equals on the update cursor?

0 Kudos
MuhammadAttique
Regular Contributor

Dear David Pike,

actually Searchcursor save stsink and tsource on the bases of query , then that tsink will search again with update cursor , if found it will change to new values of tsink and tsource until eof .

thanks for your time 

0 Kudos
DavidPike
MVP Frequent Contributor

I think you're replacing the values of tsink and tsource with the field row in the update cursor.

0 Kudos