[GIS] Using InsertCursor and SearchCursor with Nested Loops

arcpycursorloop

I have a script that's parsing through text within each record of a table/field, and inserting those values into field/records of another table. Here's an example of the text being parsed (from "Field1"):

Name Multiple Words (0/24.56) Name Multiple Words2* (96/24.56) Name Multiple Words3* (0) Name Multiple Words4* (96/12.58) Name Multiple Words5* (96/78.12) Name Multiple Words6* (0/98.32) Name Multiple Words7* (96/0) Name Multiple Words8* (0) Name Multiple Words9**

Here's the script:

import re, arcpy, sys

# Local variables:
Table2 = "D:\Source_Data_Convert.gdb\Table2"
RAW_Data = "D:\Source_Data_Convert.gdb\RAW_Data_Table2"

#Create Cursors and Insert Rows
insertcursor = arcpy.da.InsertCursor(Table2, ["Other_Stuff", "That_Stuff", "Some_More", "From", "To", "Num_One", "Num_Two", "Nums"])
with arcpy.da.SearchCursor(RAW_Data, ["Field1", "Other_Stuff", "That_Stuff", "Some_More"]) as searchcursor:
########  Start the SearchCursor Loop ###################   
        for row in searchcursor:
            try:
                Other_Stuff = row[1]
                That_Stuff = row[2]
                Some_More = row[3]
                listFrom = re.split(r'\*\s*\(.*?\)\s*', row[0])
                print listFrom

                Nums = re.findall(r'\(([^)]+)\)', row[0])
                for match in re.finditer(r'\(([^)]+)\)', row[0]):
                    parts = match.group(1).split('/')
                    print parts
                    First_Num = parts[0]
                try:
                    Second_Num = parts[1]
                except IndexError:
                    #Second_Num = None
                    Second_Num = 0
                    print "First_Num, Second_Num: ", First_Num, Second_Num

                print "Parsing Successful"

############  Start the Insertcursor Loop ############          
                for n,Value in enumerate(match): #enumerate is essentially doing a count
                    insertcursor.insertRow((Other_Stuff, That_Stuff, Some_More, listFrom[n], listFrom[n+1], First_Num, Second_Num, Nums[n]))
                    print "Data Inserted"
            except:
                pass
            else:
                break

del insertcursor
del searchcursor
del row

I can get the values to print correctly, but I can't seem to get the (2) values from the inner nested loop ("First_Num" and "Second_Num") to insert correctly with each (1) value ("Seg_Nums", etc.) from the outer loop. When I run the script, the last set of values from the inner loop get populated with each Seg_Num value from the outer loop. Hopefully my question makes sense. Basically I'm trying to populate (3) fields. One field gets the values from the outer loop, the other two fields get the values (resulting from the split) from the inner loop.

Best Answer

Assuming your regular expression is working as expected, I think you have an indentation problem wiht your second try/except. Also, I can't say it is the best way, but I might only create an insert cursor when you get to the part you want to insert.

Table2 = "D:\Source_Data_Convert.gdb\Table2"
RAW_Data = "D:\Source_Data_Convert.gdb\RAW_Data_Table2"

#Create Cursors and Insert Rows
with arcpy.da.SearchCursor(RAW_Data, ["Field1", "Other_Stuff", "That_Stuff", "Some_More"]) as searchcursor:
########  Start the SearchCursor Loop ###################   
        for row in searchcursor:
            try:
                Other_Stuff = row[1]
                That_Stuff = row[2]
                Some_More = row[3]
                listFrom = re.split(r'\*\s*\(.*?\)\s*', row[0])
                print listFrom

                Nums = re.findall(r'\(([^)]+)\)', row[0])
                for match in re.finditer(r'\(([^)]+)\)', row[0]):
                    parts = match.group(1).split('/')
                    print parts
                    First_Num = parts[0]
                    try:
                        Second_Num = parts[1]
                    except IndexError:
                       #Second_Num = None
                        Second_Num = 0
                    print "First_Num, Second_Num: ", First_Num, Second_Num

                print "Parsing Successful"


                with arcpy.da.InsertCursor(Table2, ["Other_Stuff", "That_Stuff", "Some_More", "From", "To", "Num_One", "Num_Two", "Nums"]) as ic:    
                    for n,Value in enumerate(match): #enumerate is essentially doing a count
                        ic.insertRow((Other_Stuff, That_Stuff, Some_More, listFrom[n], listFrom[n+1], First_Num, Second_Num, Nums[n]))
                        print "Data Inserted"
            except:
                pass
            else:
                break
Related Question