Referencing Multiple Data Sources with Python

2760
24
Jump to solution
08-31-2017 07:57 AM
by Anonymous User
Not applicable

*EDIT*

I marked Joshua Bixby's response as the correct answer as it solved the original issue I had, but I also want to give a special thanks to Dan Patterson for helping me figure out the tuple issue.  Thanks for everyone else for chiming in as well.  Much appreciated!  Please reference the final working script at the bottom of this request. 

Hello, I have working scripts that use UpdateCursor and InsertCursor, but all of my previous work has been in the fashion of inserting or updating records in the feature class or table that I am referencing. I'm struggling to figure out how to reference data in one feature class, and then insert entries using that information into a separate database table. I'm pretty confident that the problem I am having is very basic, but I am no expert in Python and I'm obviously missing something. Basically I want to take every entry in a feature class, and then depending on a user input, enter X number of entries into a related table for each feature in the feature class.

So say the number of features in the feature class is 10, and the user states that we need 2 records in the related table for each site, then I need the script to create 20 entries. In those 20 entries, though, I need for the script to take a number of fields from the feature class, and port them over into the appropriate fields in the related table entries.

with arcpy.da.UpdateCursor(inTable, inField) as cursor: 
     for row in cursor: 
          row[0] = name 
          cursor.updateRow(row)‍‍‍‍ ‍‍‍‍‍‍‍‍

The above code is something I'm familiar with, but again, it's only referencing one feature class, so the inField variable represents fields that are contained within the inTable variable, obviously. How do I grab fields from a feature class, create entries in a table, and then populate some of the fields in those entries based on the feature class?

Working Script:

import arcpy
numIDs = user_input  # user input variable
targetTBL = target_table  # table that rows will be inserted into
targetFields = ('SimpleID', 'ExtendedID', 'InternalID', 'ExternalID', 'Termination', 'ProjectRegion', 'ProjectState', 'ProjectName', 'ProjectCustomer', 'NodeGUID', 'CircuitID')
sourceFC = source_table  # table that information comes from
sourceFields = ('SimpleID', 'ExtendedID', 'InternalID', 'ExternalID', 'Termination', 'ProjectRegion', 'ProjectState', 'ProjectName', 'ProjectCustomer', 'GlobalID')

# Execute Insert Cursor and Search Cursor
with arcpy.da.InsertCursor(targetTBL, targetFields) as targetCursor:
    with arcpy.da.SearchCursor(sourceFC, sourceFields) as sourceCursor:
        for row in sourceCursor:
            for i in range(1, numIDs+1):
                if i < 10:
                    IDnumber = "0" + str(i)
                else:
                    IDnumber = str(i)
                InternalID = row[2]
                Termination = row[4]
                Customer = row[8]
                circuitID = "({})-({})-({})-({})".format(InternalID, Termination, Customer, IDnumber)
                targetCursor.insertRow(row + (circuitID,))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
24 Replies
by Anonymous User
Not applicable

I believe that error was referencing the insert row operation. In trying to find a solution I added the variables fo each row item thinking that it might alleviate the error, but it did not.  

Take away the 3 row variable lines and line 10 lands you at the insert row line, which is the same as row 14 in the very next example of my code.  

Sorry for the confusion, I just had some remnants of trial and error that took place after the error code was generated.  

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Does this work?

with arcpy.da.InsertCursor(targetTBL, targetFields) as targetCursor:
    with arcpy.da.SearchCursor(sourceFC, sourceFields) as sourceCursor:
        for row in sourceCursor:
            for i in range(1, numIDs+1):
                if i < 10:
                    IDnumber = "0" + str(i)
                else:
                    IDnumber = str(i)
                InternalID = row[2]
                Termination = row[4]
                Customer = row[8]
                circuitID = "({})-({})-({})-({})".format(InternalID,Termination,Customer,IDnumber)
                targetCursor.insertRow(row + tuple([circuitID]))‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
by Anonymous User
Not applicable

This is the error I get with that sample.

0 Kudos
DanPatterson_Retired
MVP Emeritus

see my example post... you can't 'tuple' a string

0 Kudos
DanPatterson_Retired
MVP Emeritus

Joining a tuple and a string 

The right way

 a= ('a', 'b', 'c')

b = 'def'

c = a + (b,)  # ---- what a difference a little comma makes ----

c
('a', 'b', 'c', 'def')

The wrong way

d = a + (b)  # ---- or a + b wrongo as well

Traceback (most recent call last):

  File "<ipython-input-11-2547ba7e6fd6>", line 1, in <module>
    d = a + (b)

TypeError: can only concatenate tuple (not "str") to tuple