How can I insert a record N amount of times using cursors?

1859
8
01-25-2017 12:05 PM
ErnestoCarreras3
Occasional Contributor

I need to generate a polygon FC using some records previously flagged as 'NUMBER_GT_NUMBER_END = 1'

These records need to be loaded into a new schema of the same FC in order to include duplicates that will be used later for other processes. If you run the code as I am giving it here, it will print out the number of records per OBJECTID. The issue is that I am able to insert only one for each case. Once added a record into the new FC schema, I want to update the SITUS_STREET_NUMBER field with the value stored in x + 1. I have been going back and forth with other approaches like using Append, Copy, among others, but the process is really slow in comparison to the one achieved so far using the da.cursors.

Example:

OBJECTID = 28964 ---> 3 records

4815

4816

4817

This means that the above polygon geometry with OBJECTID = 28964 needs to be inserted to the new FC three times and that the SITUS_STREET_NUMBER field needs to be updated with the listed values.

Thanks in advance.

Jake Skinner

Dan Patterson

0 Kudos
8 Replies
DarrenWiens2
MVP Honored Contributor

Can't you do it using a for loop and range?

>>> sample_data = [2,5,3]
... for sample in sample_data:
...     for i in range(sample):
...         print "insert"
...     print "done"
...     
insert
insert
done
insert
insert
insert
insert
insert
done
insert
insert
insert
done
0 Kudos
ErnestoCarreras3
Occasional Contributor

Darren thanks for your feedback but I am still stuck in the same step (I guess).

Not sure if you ran my script but it does a similar thing. It iterates using the xrange. In addition, it also provides me with the numbers that will be used later to update some values.

Example:

OBJECTID = 28964 ---> 3 records

4815

4816

4817

OBJECTID = 39423 ---> 999 records

1976

1977

1978

1979

1980

1981

.

.

.

.

What I am missing now is the piece of the code that will allow me to insert the rows N number of times according to this xrange.

0 Kudos
DanPatterson_Retired
MVP Emeritus

on iPad.. no download, are you trying to use an insertcursor InsertCursor—Help | ArcGIS Desktop 

0 Kudos
ErnestoCarreras3
Occasional Contributor

Yes sir

0 Kudos
DanPatterson_Retired
MVP Emeritus

Then follow Joshua's advice

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

At first glance, a couple of thoughts.  You are using both the original/older cursors as well as the newer Data Access cursors.  Although doing so is technical feasible, and possibly even supported, it creates readability and maintainability issues because the syntax for the two types of cursors are different.  I recommend just using one type of cursor, specifically the newer Data Access cursors.

Second, you appear to be re-creating the insert cursor every time you want to insert a record, even a duplicate of a previous record.  Without running your code, I may be mistaken.  If I am not, you might want to refactor the code so the insert cursor isn't being recreated if it doesn't need to be.  Creating cursors is an expensive operation, so reusing/resetting a cursor is desirable if possible.

ErnestoCarreras3
Occasional Contributor

Joshua thanks a lot for your feedback. Can you provide an example of what you are suggesting? Not sure if you notice from my script that I am not a programmerJ. I am getting a little bit confused/lost with your programming lingo. Again, thanks a lot for your feedback

0 Kudos
DarrenWiens2
MVP Honored Contributor

I believe you should generally follow this pattern, but you've got quite a few changes ahead of you.

iCur = arcpy.da.InsertCursor(featureClassCopy, fieldnames) # make before any loops
with arcpy.da.SearchCursor(featureClass, fieldnames, ...) as sCur:
    for sRow in sCur:
        for x in range(sRow[99]) # replace 99 with whatever field index your value should be
            iCur.insertRow(sRow) # insert