0

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.

2 Answers 2

1

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
1
  • DSLAMB apologies for not responding. I gave up on it for a while then came back to it at ta later time. Of course I forgot to post the final solution. I appreciate your assistance Oct 31, 2017 at 20:12
0

Here is the final solution I came up with in case it can help someone else.

import re, arcpy, sys

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

with arcpy.da.InsertCursor(Table2, ["Other_Stuff", "That_Stuff", "Some_More", "From_Node", "To_Node", "Seg_Nums"]) as insertcursor:
    with arcpy.da.SearchCursor(RAW_Data, ["Field1", "Other_Stuff", "That_Stuff", "Some_More"]) as searchcursor:  
        for row in searchcursor:
            try: 
                Other_Stuff = row[1]
                That_Stuff = row[2]
                Some_More = row[3]
                listFrom_Node = re.split(r'\*\s*\(.*?\)\s*', row[0])
                print listFrom_Node

                Seg_Nums = re.findall(r'\(([^)]+)\)', row[0])
                print "Seg_Nums =", Seg_Nums

                for n,Value in enumerate(listFrom_Node):
                    insertcursor.insertRow((Other_Stuff, That_Stuff, Some_More, listFrom_Node[n], listFrom_Node[n+1], Seg_Nums[n]))
                    print "Data Inserted"
            except:
                pass
            else:
                break

del insertcursor
del searchcursor
del row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.