Select to view content in your preferred language

Example using search cursor to copy data from cells in attribute table A to attribute table B

3011
26
Jump to solution
09-05-2023 12:12 PM
MPach
by
Occasional Contributor II

Does anyone have an example of how to copy data from a cell in attribute A to a cell in Attribute B?

Ideally, here's an example of what I would like to do.

1) Select the two features in the map I'd copy data from

2) From Feature_A Copy cell from column1, column2, and column3

3) Paste data from step 2 into Feature_B cells for column3, column4, and column5

I didn't see an example like this, which seems like a pretty basic use of the search cursor. 

Thanks in advance!

0 Kudos
26 Replies
MPach
by
Occasional Contributor II

Right, that's what my intention is and I knew when I said that about multiple features it would be confusing. I intend to manually select one feature row from the copy_layer and paste the from the columns that I need to exactly one feature row in the paste_layer and then repeat that many times. I was trying to explain that if I used the first script provided the data in the paste_layer would keep getting overwritten upon each use because the data being copied from copy_layer was getting pasted to every record in the paste_layer instead of just the record that I had selected. I hope that makes more sense now.

I'm looking into using your idea of getSelectionSet () method on the arcpy.mapping.Layer layer object to get it to work.

0 Kudos
MPach
by
Occasional Contributor II

and @JohannesLindner  would something like this work or am I asking too much?

 

copy_layer = "D:\GISData\Projects\Water\Dashboards\SewerCapacityNorthModelData\Python Tool Test\Phase1Data.shp"
copy_fields = ["capdepth", "capflow"]
paste_layer = "D:\GISData\Projects\Water\Dashboards\SewerCapacityNorthModelData\Python Tool Test\DataLayerPaste.shp"
paste_fields = ["capdep25","capflo25"]

#set lyr file to copy layer
lyrSelect = arcpy.mapping.Layer(paste_layer)

#access the selected row for the 
lyrSelected = lyrSelect.getSelectionSet()

# read the first copy feature
data = [row for row in arcpy.da.SearchCursor(copy_layer, copy_fields)][0]

# paste the copy feature's data into each paste feature
with arcpy.da.UpdateCursor(lyrSelected, paste_fields) as cursor:
    for row in cursor:
        cursor.updateRow(data)

 

0 Kudos
MPach
by
Occasional Contributor II

guess not. 

Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
RuntimeError: 'in_table' is not a table or a featureclass

0 Kudos
MPach
by
Occasional Contributor II

Johannes I documented all the steps that I am using with the first script to show the results to so that you can better understand what I was trying to describe as the problem. 

Step 1:

Select 1 row from each table

MPach_0-1694545509052.png

MPach_1-1694545534780.png

Step 2 run the script:

MPach_2-1694545575193.png

Here’s what the result looks like in the table that the data was pasted to:

MPach_3-1694545624531.png

The data is not pasted to just one record it's pasted to all of them, which means that if I ran the tool a second time for a new segment it would just overwrite all the data from the first run.

I hope that helps explain the problem I was trying to describe.

Thanks, 

Mark

 

 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Oh, I've never seen that done that way. That is slick.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Maybe this is what you're looking for then? You might have to save the mxd after you make the selection and before you run the script. I have not tested this, but I expect it to copy the identified fields of the one record you selected in the copy layer and paste those values into the identified fields of every selected record in the paste layer.

You could also put this logic into a script tool or Python Toolbox, which would let you access the layers in the map directly without saving the mxd.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Path\to\Map Document\your_filename.mxd")
try:
    df = arcpy.mapping.ListDataFrames(mxd, "Name of DataFrame Here")[0]
    copy_layer = arcpy.mapping.ListLayers(mxd, "CopyLayerName", df)[0]
    copy_fields = ["columns1", "column2", "column3"]
    paste_layer = arcpy.mapping.ListLayers(mxd, "PasteLayerName", df)[0]
    paste_fields = ["column3", "column4", "column5"]

    if len(copy_layer.getSelectionSet()) == 1 and len(paste_layer.getSelectionSet()) > 0:
        copy_data = [row for row in arcpy.da.SearchCursor(copy_layer, copy_fields)][0]
        with arcpy.da.UpdateCursor(paste_layer, paste_fields) as cursor:
            for row in cursor:
                cursor.updateRow(copy_data)
    else:
        raise Exception("One of the layers does not have the correct number of selected records.")
finally:
    del mxd
MPach
by
Occasional Contributor II

Thank you. I'll play around with this and give it a shot. 

0 Kudos