Select to view content in your preferred language

Search and Update Cursors

2755
5
Jump to solution
10-09-2013 12:40 PM
ionarawilson1
Deactivated User
I need to use search and update cursors to get the values from one feature class to another. Before this code worked because I had only one record to be updated, but now I may have more than one. How can I change it to let it update many records? Thanks

    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)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ionarawilson1
Deactivated User
This worked, I just had to create temporary layers, instead of feature classes

# Create new field mappings and addd both feature classes  fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(Input_Polygons) fieldmappings.addTable(Counties)  # create output feature for spatial join outstewardshipcounties = os.path.join(arcpy.env.scratchGDB, "StewardshipCounties")  #run spatial joint tool  arcpy.SpatialJoin_analysis(Input_Polygons, Counties, outstewardshipcounties , "#", "#", fieldmappings, "HAVE_THEIR_CENTER_IN")  #create dictionary  path_dict = { } rows = arcpy.SearchCursor(outstewardshipcounties) for row in rows:     keyrow = row.getValue("ObjectID")     valrow = row.getValue("FIPS_TXT")     path_dict[keyrow] = valrow  urows = arcpy.UpdateCursor(Input_Polygons) for urow in urows:     upkey = urow.getValue("ObjectID")     if upkey in path_dict:         urow.setValue("County", path_dict[upkey])         urows.updateRow(urow) del row, rows, urow, urows

View solution in original post

0 Kudos
5 Replies
ionarawilson1
Deactivated User
This worked, I just had to create temporary layers, instead of feature classes

# Create new field mappings and addd both feature classes  fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(Input_Polygons) fieldmappings.addTable(Counties)  # create output feature for spatial join outstewardshipcounties = os.path.join(arcpy.env.scratchGDB, "StewardshipCounties")  #run spatial joint tool  arcpy.SpatialJoin_analysis(Input_Polygons, Counties, outstewardshipcounties , "#", "#", fieldmappings, "HAVE_THEIR_CENTER_IN")  #create dictionary  path_dict = { } rows = arcpy.SearchCursor(outstewardshipcounties) for row in rows:     keyrow = row.getValue("ObjectID")     valrow = row.getValue("FIPS_TXT")     path_dict[keyrow] = valrow  urows = arcpy.UpdateCursor(Input_Polygons) for urow in urows:     upkey = urow.getValue("ObjectID")     if upkey in path_dict:         urow.setValue("County", path_dict[upkey])         urows.updateRow(urow) del row, rows, urow, urows
0 Kudos
AliciaMein
Occasional Contributor
I have a use for the update and search cursors.
first question:  why are you using the os.path in your script instead of the addjoin management function?
Alicia
0 Kudos
ionarawilson1
Deactivated User
Add join is to join layers and the os.path.join is just adding the strings to create a layer in that scratch workspace
0 Kudos
AliciaMein
Occasional Contributor
I understand the Arcpy module and navigate the help at esri well but is there documentation on the os module?
I will post a new thread for my update and search cursor issue and look forward to seeing a reply from you.
Thanks,
Alicia
0 Kudos
ionarawilson1
Deactivated User
0 Kudos