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

3871
26
Jump to solution
09-05-2023 12:12 PM
MPach
by
Frequent Contributor

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
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You can accomplish that with the arcpy.da.*Cursor classes:

copy_layer = "Layer A"
copy_fields = ["columns1", "column2", "column3"]
paste_layer = "Layer B"
paste_fields = ["column3", "column4", "column5"]

# 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(paste_layer, paste_fields) as cursor:
    for row in cursor:
        cursor.updateRow(data)

 


Have a great day!
Johannes

View solution in original post

26 Replies
AlfredBaldenweck
MVP Regular Contributor

You just have to nest it with either an update cursor or insert cursor, depending on if the target row already exists.

Rough, untested code:

#search through rows
with arcpy.da.SearchCursor(fc1, [fields]) as cursor:
    for row in cursor:
        #do something to match the rows, probably a where clause along these lines 
        with arcpy.da.UpdateCursor(fc2, [fields] where_clause = 'field = row[2]') as c2rsor:
            for r2w in c2rsor:
                 r2w = row
                 c2rsor.updateRow(r2w)

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

You can accomplish that with the arcpy.da.*Cursor classes:

copy_layer = "Layer A"
copy_fields = ["columns1", "column2", "column3"]
paste_layer = "Layer B"
paste_fields = ["column3", "column4", "column5"]

# 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(paste_layer, paste_fields) as cursor:
    for row in cursor:
        cursor.updateRow(data)

 


Have a great day!
Johannes
DavidPike
MVP Frequent Contributor

Prefer this over instantiating a cursor for each row.

BlakeTerhune
MVP Regular Contributor

Agreed, this is my preferred method as well. It's also the methodology @RichardFairhurst uses for Turbo Charging Data Manipulation with Python Curso... - Esri Community

MPach
by
Frequent Contributor

Thanks Blake, I saved that link to my favorites folder. Much appreciated!

 

0 Kudos
MPach
by
Frequent Contributor

Thank you Johannes. Cheers!

0 Kudos
MPach
by
Frequent Contributor

Johannes or @DavidPike is there anyway to get this script to work based on the records selected in both the copy layer and the paste layer. I only want to paste the information selected on a one to one basis instead of a one to many or one to all. Right now it appears it's using the selected record in the copy layer, but it is pasting that data to all the records in the paste layer. I thought that if I selected the records in each table before running the script that it would respect that selection, but it doesn't look like it's functioning that way. 

Thank you so much for you help,

Mark

0 Kudos
JohannesLindner
MVP Frequent Contributor

You're right, it takes only the first feature in the copy layer and pastes its attributes into all features of the paste layer.

 

If you want to do a one-to-one copy/paste based on selection, you can do it like this:

# read all copy features
data = [row for row in arcpy.da.SearchCursor(copy_layer, copy_fields)]

# paste their attributes into the paste features
with arcpy.da.UpdateCursor(paste_layer, paste_fields) as cursor:
    for i, row in enumerate(cursor):
        try:
            cursor.updateRow(data[i])
        except IndexError:
            print("You selected too few features in the copy layer, only {} features in the paste layer were processed!".format(i))
            break
if i+1 < len(data):
    print("You selected too many features in the copy layer, {} features were not copied!".format(len(data)-i-1))

 

Note that both layers will be processed in order of OBJECTID. So the attributes of the copy feature with the lowest OID will be pasted to the paste feature with the lowest OID.


Have a great day!
Johannes
MPach
by
Frequent Contributor

Thank you Johannes. Very much appreciated! That saves me a bunch of time. 

 

0 Kudos