0

I have to join multiple tables to a shapefile using a common field. I want to script this in python (i am using ArcGIS 10.1). Below is the script i am using.

I am calling the tables one by one and merging them to the shape. I would like to put this in a for loop so the script reads all the tables (that have prefix dTable) and merge them to the shape.

#Redefine the workspace to the geodatabase

env.workspace = os.path.dirname(os.path.dirname(__file__))+'\\'+ out_name +'.gdb'
print "Workspace for GDB:"+ env.workspace

################################################################################
#STEP 5: Start Joining Tables
# Set local variables

featureName = "temp_Streets"
layerName="Streets2"

outFeature = "temp_Streets2"
expression = "temp_Streets.FUNC_CLASS < 99"

################################################################################
# Exclude centriod connectors from the shapefile before joining
try:
     # Make a layer from the feature class
    arcpy.MakeFeatureLayer_management(featureName,layerName)
    print "Converting to Layer Done !"

    # Select desired features from street layer
    arcpy.SelectLayerByAttribute_management(layerName, "NEW_SELECTION", expression)
    print "Selection Done !"

    # Copy the layer to a new permanent feature class
    arcpy.CopyFeatures_management(layerName, outFeature)
    print "Copy to new layer Done !"

    print "Step 5: Creating Streets file Done !"
    print "#################################################"
    arcpy.AddMessage("Creating Streets file Done !")
except:
    print arcpy.GetMessages()
################################################################################
joinTable1 = "dTable_HBW_csv"
layername1="j_Streets1"
outFeature1 = "temp_j_Streets1"
joinField = "CONNECT"

# Join the first CSV file
try:
     # Make a layer from the feature class
    arcpy.MakeFeatureLayer_management(outFeature,layername1)
    print "Converting to layername1 Done !"

    # Join the feature layer to a table
    arcpy.AddJoin_management(layername1, joinField, joinTable1, joinField, "KEEP_ALL")
    print "Join 1 Done !"

    # Copy the layer to a new permanent feature class
    arcpy.CopyFeatures_management(layername1, outFeature1)
    print "Copy to new layer 1 Done !"

    print "Step 5a: Joining Streets 1 file Done !"
    print "#################################################"
    arcpy.AddMessage("Joining Streets 1 file Done !")

except:
    print arcpy.GetMessages()
###############################################################################
joinTable2 = "dTable_HBO_csv"
layername2="j_Streets2"
outFeature2 = "temp_j_Streets2"
joinField2 = joinTable1+"_CONNECT"

# Join Second CSV file
try:
     # Make a layer from the feature class
    arcpy.MakeFeatureLayer_management(outFeature1,layername2)
    print "Converting to layername2 Done !"

    # Join the feature layer to a table
    arcpy.AddJoin_management(layername2, joinField2, joinTable2, joinField, "KEEP_ALL")
    print "Join 2 Done !"

    # Copy the layer to a new permanent feature class
    arcpy.CopyFeatures_management(layername2, outFeature2)
    print "Copy to new layer 2 Done !"

    print "Step 5b: Joining Streets 2 file Done !"
    print "#################################################"
    arcpy.AddMessage("Joining Streets 2 file Done !")

except:
    print arcpy.GetMessages()
2

One way to do it would be to create 4 lists containing the feature dataset names, like so: You can add to these lists and possibly use list comprehensions to automatically create some of them.

joinTable = ["dTable_HBW_csv", "dTable_HBO_csv"]
layername = ["j_Streets1", "j_Streets2"]
outFeature = ["temp_j_Streets1", "temp_j_Streets2"]
joinField = ["CONNECT", joinTable[0] + "_CONNECT"]

From there, you can use the build in zip() function to "merge" them together so you're only using 1 for loop.

#Pseudo code..You'll need to play around with this since the output differs from input.
#Possibly consider making an input list as well. Lot's of ways to do stuff in Python.

for table,layer,outFC,field in zip(joinTable, layername, outFeature, joinField):
    arcpy.MakeFeatureLayer_management(outFC, layer)
    arcpy.AddJoin_management(layer, field, table, field, "KEEP_ALL")
    arcpy.CopyFeatures_management(layer, outFC)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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