Arcpy for loop gives n-1 results

2832
22
Jump to solution
02-10-2017 01:02 PM
Nicole_Ueberschär
Esri Regular Contributor

I have a problem with my for loop. 

In an array profile_nrs all the numbers are stored I want to create points for in an existing feature class. When I print these values I get the expected nr of values (and also the correct values). Now I am creating points with an insertCursor and print in the end which point nr was added. I get all the (in this case) 18 points printed. But when I look into my file I receive only 17 points. Any advise is highly appreciated. I don't see any potential cause for this "misbehavior". My feeling tells me that it must not be looping through the numbers itself but through the index but then I wouldn't get nr 18 printed, would I?

Here the main part of the module: 

for profile in profile_nrs:
    newPoint=arcpy.da.InsertCursor(points, fillnames)
    newPoint.insertRow(fillValues)
    print("point "+str(profile) +" added to All_points")‍‍‍‍

Inside the for loop I have a couple of other loops to get the fillnames and fillvalues but I don't see anywhere how this could influence the output. Especially if it says it added nr 18 but obviously it didn't...

Thanks in advance for helping me with this. 

0 Kudos
22 Replies
JoshuaBixby
MVP Esteemed Contributor

Line #4 will generate a StopIteration error if the cursor has been exhausted, i.e., iterated over and not reset, or the cursor has no records in the first place.  Given the code, I am thinking your cursor isn't returning any records.

I know Python is new to you, but you have to learn to walk before you can run.  At some point, you need to understand why people are making specific suggestions and how the code they offer works.  In terms of Python, there is nothing special about ArcPy.  When some ArcPy code generates an error, such as StopIteration, that error likely isn't unique to ArcPy.  ArcPy cursors are iterables, so the question to ask yourself is why does a Python iterable generate a StopIteration error?  The answer to that question will lead you to the answer to why your search cursor is generating that error.

Copying someone's code and using it without understanding why it works is bound to cause problems, if not immediately than definitely down the road.  I understand that dissecting other's code to learn why it works takes time, but it is one of those pay now or pay later situations.  I have always preferred to pay now because I never seem to have more time available later.

The GeoNet community wants to help, as evidenced by the 18 replies and growing, but I know personally that I am burned out on this thread.  I feel there are too many methodology and technical issues colliding with each other, and I don't know where to start commenting any more.  My suggestion for now is to take a step back and break your code into smaller functional groups.  Get each group or block of code working, and understand why it works, and then start stacking the blocks of code on top of each other.

Best of luck, and keep at it.  Python is a wonderful language and ArcPy can be a very powerful tool.

0 Kudos
Nicole_Ueberschär
Esri Regular Contributor

Thank you Joshua, understanding what the script is doing was the intention of my last comment. 

0 Kudos
RichardFairhurst
MVP Honored Contributor

Nicole:

Line 15 adds the station field as the first field in the list, because this is field holding the unique dictionary key value used match records between each table per your description of what you wanted to do.  The field list includes the station twice, once for the dictionary key and once for the value of the field for a given row, which is important later when the station field has to be in the position it exists relative to other fields to hold all the values of a row object that is passed to the insert cursor.

Line 19 reads the records of the search table that contains the table names.

Lines 18-23 - you are correct that the dictionary is adding each station only the first time it is encountered to the dictionary and the key value is the concatenation of the two fields that make up your table names, per your description of what you had set up.

For the modification of the code, I did not test using the next method.  Go back to using the for loop and break out of it once you have created a feature for the table.  I.e.:

for station in sorted(stationDict):
    table_name = stationDict[station]
    with arcpy.da.SearchCursor(table_name, pfieldnames) as pCursor:
        for pRow in pCursor:
            stationpointDict[station] = pRow
            pfieldsvalues = pRow
            ShapeVal = [pfieldsvalues[4], pfieldsvalues[5]]
            fillValues = [ShapeVal] + list(pfieldsvalues)
            print(fillValues) 
            newPoint.insertRow(fillValues)
            print("point " + str(station) + " added to All_points")
            break
   del pCursor