From arcpy.SearchCursor and arcpy.UpdateCursor to  arcpy.da.SearchCursor and arcpy.da

3085
8
Jump to solution
07-16-2013 12:53 PM
ionarawilson1
Occasional Contributor III
I wrote code using the arcpy.searchCursor and arcpy.UpdateCursor but want to switch to arcpy.da.SearchCursor and arcpy.da.UpdateCursor. I tried to change it but I get an error message in the lines after the updatecursor. Does anybody can tell me what I am doing wrong? Thank you!


I changed from this
    srows = arcpy.SearchCursor("Countieslayer", )      obj_id = 1      for srow in srows:           # assign a variable for the value of srow.getValue("FIPS_TXT)         countycode = srow.FIPS_TXT          # Get a collection of rows for the update cursor          # create the update cursor         urows = arcpy.UpdateCursor("Stewardship")          # cycle through the rows (in this case only 1)         # to actually update the row in the cursor         # with the values obtained from the search cursor         for urow in urows:             urow.County = countycode              urows.updateRow(urow)          obj_id += 1 


to this:

  with arcpy.da.SearchCursor("Countieslayer", ("FIPS_TXT")) as cursor:       obj_id = 1      countycode = ""      countycode2 = ""      for row in cursor:           # assign a variable for the value of srow.getValue("FIPS_TXT)          countycode = row[0]          # Get a collection of rows for the update cursor          # create the update cursor         with arcpy.da.UpdateCursor("Stewardship", ("County")) as cursor:          # cycle through the rows (in this case only 1)         # to actually update the row in the cursor         # with the values obtained from the search cursor             for row in cursor:                 countycode2 = countycode                 countycode2 = row[0]                  cursor.updateRow(row)          obj_id += 1


And I get this error message:

PYTHON ERRORS:
Traceback Info:
  File "D:\ArcGISData\SARS\Python_10April2013\BoundaryReporting_26March2013_changes_july2013_added_domain_changed_again.py", line 226, in <module>
    for row in rows:

Error Info:
     <type 'exceptions.AttributeError'>: Object: Error in parsing arguments for AddMessage


For this part:

 rows = arcpy.SearchCursor("Stewardship")     obj_id = 1     for row in rows:                    FFYtxt = row.getValue("FFY")                   Countytxt = row.getValue("County")                    arcpy.AddMessage(Countytxt)       rowsffycounty = arcpy.UpdateCursor("Stewardship")     for row in rowsffycounty:         row.FFYCounty = FFYtxt + "-" + Countytxt         #FFYCountytxt = row.FFYCounty         rowsffycounty.updateRow(row)     obj_id += 1 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
This could be one way of doing it.

    countycode = tuple(arcpy.da.SearchCursor("Countieslayer", "FIPS_TXT"))[0][0]     urows = arcpy.da.UpdateCursor("Stewardship", "County")     for urow in urows:         urow[0] = countycode         urows.updateRow(urow)

View solution in original post

0 Kudos
8 Replies
MathewCoyle
Frequent Contributor
First off, you are overwriting your cursor in the middle of iterating through it as well as your row objects, this will cause all sorts of problems. Additionally, your indentation is a little inconsistent, and creating a cursor for every row in another cursor is terribly inefficient.
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Mathew. So is it better to keep the two cursors separate? Is this better?

    srows = arcpy.SearchCursor("Countieslayer")

    obj_id = 1

    for srow in srows:


        # assign a variable for the value of srow.getValue("FIPS_TXT)
        countycode = srow.FIPS_TXT

        # Get a collection of rows for the update cursor


    obj_id += 1
        # create the update cursor
    urows = arcpy.UpdateCursor("Stewardship")

        # cycle through the rows (in this case only 1)
        # to actually update the row in the cursor
        # with the values obtained from the search cursor
    for urow in urows:
        urow.County = countycode
    urows.updateRow(urow)


    del srow, srows, urow, urows #Clean up cursor objects
0 Kudos
MathewCoyle
Frequent Contributor
There are better ways to accomplish what you are trying to do but you have the gist of it. Why do you have your obj_id counter? It doesn't seem to be used for anything in your code. For your two datasets "Countieslayer" and "Stewardship" do these both have just one row in them?
0 Kudos
ionarawilson1
Occasional Contributor III
It was just a counter variable, I should have called it something else. You are right, I don't even need the counter variable, since counties only has 1 record and the stewardship layer has many records but I run the tool with only one record selected. Thanks. What would be the best way to write these cursors and also how can use the da.cursors in this case? Thanks a lot!

    srows = arcpy.SearchCursor("Countieslayer")
    for srow in srows:


        # assign a variable for the value of srow.getValue("FIPS_TXT)
        countycode = srow.FIPS_TXT

        # create the update cursor
    urows = arcpy.UpdateCursor("Stewardship")

        
    for urow in urows:
                urow.County = countycode

                urows.updateRow(urow)

0 Kudos
MathewCoyle
Frequent Contributor
This could be one way of doing it.

    countycode = tuple(arcpy.da.SearchCursor("Countieslayer", "FIPS_TXT"))[0][0]     urows = arcpy.da.UpdateCursor("Stewardship", "County")     for urow in urows:         urow[0] = countycode         urows.updateRow(urow)
0 Kudos
ionarawilson1
Occasional Contributor III
Thank you Matthew. Now, why do I have to convert it to tuple if it is already returning a tuple? and what is the [0][0] for? thanks
0 Kudos
MathewCoyle
Frequent Contributor
This line just takes the first row of the cursor without having to go through getting a row object. The cursor itself is a generator, which needs to be converted to a tuple to get the row values. The first [0] index is the first row, of which there should only be one anyway, the next [0] index is the first attribute, again of which there is only one, the FIPS_TXT field.

countycode = tuple(arcpy.da.SearchCursor("Countieslayer", "FIPS_TXT"))[0][0]
0 Kudos
ionarawilson1
Occasional Contributor III
Cool, thank you for the thorough explanation!
0 Kudos