How to store first iteration of loop in a defined location and the rest of the iterations to append to first iteration results?

1606
14
Jump to solution
05-10-2018 11:16 AM
VishalShah2
Occasional Contributor II

Hello,

I am working with a for loop where a summary statistics is created. I am able to store the output to a custom file gdb using TableToTable conversion. However, I'm not able to append data to it on the following iterations. How can I go about this?

if hubDict[0]:
    arcpy.TableToTable_conversion(in_rows, out_path, out_name)
else:
    arcpy.Append_management(inputs, target, "NO_TEST")
    

hubDict is a dictionary i have created earlier on. in_rows, out_path, out_name, inputs, target are all variables that are defined to the correct files and output locations.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

After looking at your code, I changed my approach to take the path of least modifying your code:

for i, hub in enumerate(hubDict):
    valueHub = dict.get(hubDict, hub)
    hubQuery = "HubName = " + "'" + str(valueHub) + "'"
    nodeQuery = "ProjectHub = " + "'" + str(valueHub) + "'"
    hubLayer.definitionQuery = hubQuery
    nodeLayer.definitionQuery = nodeQuery
    
    if i == 0:
        in_rows = 'table'
        out_path = folderPath + '/' + 'Results.gdb'
        out_name = 'Results'

        arcpy.TableToTable_conversion(in_rows, out_path, out_name)

    else:
        inputs = 'table'
        target = folderPath + '/' + 'Results.gdb' + '/' + 'Results'

        arcpy.Append_management(inputs, target, "NO_TEST")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
14 Replies
JoshuaBixby
MVP Esteemed Contributor

Are you getting an error or unexpected results?  If the former, what error, exactly (include the traceback).  If the latter, what do you expect and what are you getting?

0 Kudos
VishalShah2
Occasional Contributor II

Hi Joshua,

Sorry about the delay in getting back to you. So my script first creates a dictionary per feature in a feature class. Then the script runs a for loop per feature in the feature class using the values in the dictionary. Rather than exporting several tables and their excel sheets, I am trying to create a table with the first iteration using table to table (conversion) and then for the rest of the iterations, I want their tables to append to the created table from the first iteration. The way I have it set up is the following:

if hubDict[0]:
    in_rows = 'table'
    out_path = folderPath + '/' + 'Results.gdb'
    out_name = 'Results'

    arcpy.TableToTable_conversion(in_rows, out_path, out_name)

else:
    inputs = 'table'
    target = folderPath + '/' + 'Results.gdb' + '/' + 'Results'

    arcpy.Append_management(inputs, target, "NO_TEST")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The Results table is being created on the first iteration but on the second one, the data isn't getting appended.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Are you sure Append is being executing?  If so, capture the Result—Help | ArcGIS Desktop and see what messages it is giving.

0 Kudos
VishalShah2
Occasional Contributor II

It's not being executed because of the else statement. I don't think I set that up correctly nor do I know how to fix that. For some reason that is not registering on the second iteration and would love it if you have any ideas on how to approach this scenario..

0 Kudos
VishalShah2
Occasional Contributor II

When I run the steps outside of the main for loop, the append is being executed correctly. My issue is not sure how to script it for the script to read it as the first iteration or Xth iteration. I want the first iteration to create the table and the following iterations to be appended to it.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

How are you iterating over hubDict?  If hubDict is a list, which it appears to be, you have to loop over it in some way, shape, or form. Knowing a bit more about the structure of hubDict would be helpful too.

0 Kudos
VishalShah2
Occasional Contributor II

So hubDict is actually a dictionary. To loop over over it, I'm doing the following:

for hub in hubDict:
    valueHub = dict.get(hubDict, hub)
    hubQuery = "HubName = " + "'" + str(valueHub) + "'"
    nodeQuery = "ProjectHub = " + "'" + str(valueHub) + "'"
    hubLayer.definitionQuery = hubQuery
    nodeLayer.definitionQuery = nodeQuery

hubLayer and nodeLayer are variables that point to the hub and node layers in my data frame. After this, in the loop I do a network analysis and use that to do some joins and run a summary statistics. I'm trying to use the output table from the summary statistics to get into the results FGDB.

0 Kudos
VishalShah2
Occasional Contributor II

Apparently the append worked this time around but the original data was overwritten with the data that was appended. I think the issue lies with how the if statement is set up in the for loop. How would you set it up?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Prior to Python 3.6 (one could argue it is really prior to 3.7, but that is a separate discussion), insertion order was not preserved with Python dictionaries.  If the "first" record in the dictionary means something special, you should either be using an OrderedDict or some other data structure that does maintain insertion order.

If insertion order doesn't matter and the first item is simply meant to create the table while the other items are to get appended, then it simplifies the situation a bit.  Does your first code snippet come immediately after your second one or is there other code involved? 

0 Kudos