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

3358
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
26 Replies
MPach
by
Frequent Contributor

I see how that is different, but I'm not sure if that will do what I'm looking for. I need to be able to select one field in the copy_layer and paste the values of those copy_fields to just a single asset in the paste_layer that I would be selected before the script is run. 

Example:

Selected field from Copy_Layer

MPach_0-1694445823346.png

Paste that info in the copy_layer that is selected

MPach_1-1694445993240.png

Is there a way to do that? I'm asking because it's possible that the the OBJECTIDs for each of these are not correlated correctly where the lowest OID from the copy_layer will match with the correct feature from the paste_layer. i.e. spatially, feature 001 from the copy layer may correlate with feature 099 from the paste layer , which would mean there a different feature with a lower OBJECTID like 040 from the paste_layer would match with with 001 from the copy layer. 

 

  

0 Kudos
BlakeTerhune
MVP Regular Contributor

You need a key field to relate tables like this. Agreed that ObjectID is not a good key field. Do you have a field that relates a particular record from the copy layer to the paste layer?

0 Kudos
MPach
by
Frequent Contributor

Unfortunately, I don't in this case, which is why I need to use a manual selection from the map or from the attribute tables. If I had a key field to relate I wouldn't need to this 😞

0 Kudos
BlakeTerhune
MVP Regular Contributor

If you need to run this as a script instead of a toolbox, you can access the selected records of a layer using the getSelectionSet() method on the layer object.

Layer—ArcGIS Pro | Documentation

You'll use some other arcpy.mp stuff to get to the layer you want in the map you want.

EDIT:

Sorry, I forgot you're using ArcMap. You can still get the selection set of a layer with arcpy.mapping

Layer—ArcMap | Documentation (arcgis.com)

MPach
by
Frequent Contributor

@BlakeTerhune or @JohannesLindner do you know of an example using getSelectionSet ()  that might work for what I'm attempting to do. I was having trouble finding one that uses this method to get the selection of the layer and then write the data to the selected fields in that layer. I'm really surprised there isn't something out there already that does something like this because this is a fairly common process to perform. 

I think the problem with this method is that it returns the layers selection as a Python set or object ID's, so I'm not sure it will work. 

Thanks again for all your help thus far, 

Mark

0 Kudos
MPach
by
Frequent Contributor

It seems a little mind blowing that this can't be done in Python at this point. 

0 Kudos
JohannesLindner
MVP Frequent Contributor

But doesn't my first script solve this case? Just select both features and run the script. The script will take the first feature of the copy layer (which has only one feature selected, so it doesn't matter) and copies it to all selected features in the paste layer (which also has only one feature selected), so you get a 1-to-1 copy.


Have a great day!
Johannes
0 Kudos
MPach
by
Frequent Contributor

Hmmmm, I don't think it does though unless I'm not understanding something. Maybe I'm not stating what I am trying to accomplish clearly. 

The first script copies the data from layer A and pastes it to all the records in layer B. I was hoping to find a way to copy the record from layer A and only paste that record (no other records) to a record that I have preselected in the attribute table in layer B.

Does that make sense?  

Thanks, 

Mark

0 Kudos
MPach
by
Frequent Contributor

Maybe this will help. If I used the first script I couldn't use it a second time for a different record because the doing so would cause the data from the first use to be overwritten for the next use. Does that help?

I intend to use the script for multiple features in the copy_layer and the paste_layer

Mark

0 Kudos
BlakeTerhune
MVP Regular Contributor

Without a key field, you cannot do multiple features. You would have to manually select exactly one in each table to copy the values. Otherwise, there's no way for the code to match to the correct record.

0 Kudos