Select to view content in your preferred language

Copy/duplicate rows with arcpy

649
1
11-20-2023 07:32 AM
IlkaIllers1
Frequent Contributor

I have a table (no spatial data) of cadastral data indicating monuments, where several plots have been combined into one single row. I want to disentangle them to join them with the base cadastral set. I would rather not do this manually. I have been trying to figure out a way to make duplicate rows with arcpy based on the number of "," in one field. This is how far I got:

with arcpy.da.UpdateCursor('input_fc','plots') as uCur:
    for row in uCur:
        list = []
        if ',' in row[0]:
            number = row[0].count(',')
            r = range(1,number,1)
            for i in r:

   

and that is where I got stuck trying to figure out how to tell it to duplicate the row as many times as there is a comma. Does anything like that exist?

0 Kudos
1 Reply
AlfredBaldenweck
MVP Regular Contributor

You want an Insert Cursor, not an update cursor. Update is for editing rows that are already in the table, insert is for adding new ones.

I wrote this without testing, but the basic workflow is:

  1. Read the table and load the row (a tuple with only 1 value) into a list of tuples.
  2. With the insert cursor, count the number of commas in the first value of each row in the list.
  3. If the number is greater than 1, insert the row X many times.
    1. I really am not sure of the math here, so you'll have to play around a bit to make sure it's right.

Also, am not sure if you're putting the rows into the same feature class or a new one, but either way you want an insert cursor.

 

rowList = []
with arcpy.da.SearchCursor(input_fc, 'plots') as sCur:
    for row in sCur:
        rowList.append(row)


with arcpy.da.InsertCursor('input_fc','plots') as iCur:
    for row in rowList:
        number = row[0].count(',')
        if  number > 1:
            for i in range(1, number):
                iCur.insertRow(row)

 

I also recommend asking python question in the Python Questions board for faster responses.

0 Kudos