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.
Solved! Go to Solution.
Insertion order doesn't matter as long as the first item creates the table while the remaining items append to it. Not entirely sure what you mean by code snippets and what comes after but I do some analysis work after I start the iteration through the dictionary using network analysis and the last step in the iteration is what this thread is for, either creating the table or appending to it and then repeating the steps. Hope that helps.
You have provided two code snippets, or probably better stated code fragments, during our discussion. Between the two different blocks of code, there is only a single variable that is in common, hubDict. Since I have no idea how the two blocks of code relate to each other, I can't provide specific suggestions on how to loop over hubDict and accomplish what you want.
Overall, I suggest you use dict.iteritems() to return an iterator of the items in hubDict. You can use next() to retrieve the first item and build the table, and then loop over the rest of the iterator with for to append the rest to the tables.
Ah...I get where you were coming from. For the code snippets, here is the overall picture:
for hub in hubDict:
valueHub = dict.get(hubDict, hub)
hubQuery = "HubName = " + "'" + str(valueHub) + "'"
nodeQuery = "ProjectHub = " + "'" + str(valueHub) + "'"
hubLayer.definitionQuery = hubQuery
nodeLayer.definitionQuery = nodeQuery
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")
I've never worked with dict.iteritems() or next(). How would you put that together with the code snippet I have?
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")
Thank you so much Joshua!!