1

I have two sets of shapefiles that I would like to join based on a similar field. They are all stored in a path folder which is stored as the variable "steps".

One group of shapefiles are polygon shapefiles named something like "polygonA_footprints.shp", "polygonB_footprints.shp" , "polygonC_footprints.shp"

The other group are a set of points shapefile named something like "polygonA_points.shp" "polygonB_points.shp", "polygonC_points.shp"

Both groups have the same field "PID".

I'd like to join the .dbf table from the points.shp layer to their matched polygon.shp.

The following is my code:

arcpy.env.workspace = steps
arcpy.env.overwriteOutput = True
shplist = arcpy.ListFiles("*footprints.shp")
tablelist = arcpy.ListFiles("*points.dbf")
for shp in shplist:
    sprefix = shp.rsplit("*footprints.shp")[0]
for t in tablelist:
        tprefix = t.rsplit("*points.dbf")[0]
if sprefix == tprefix:
        arcpy.AddJoin_management(shp,"PID",t,"PID")

Running the code in PyCharm does not produce any error messages, however when I open the polygon.shp in ArcMap the join is not complete. I've tried using "*footprints.dbf" and "*footprints.shp" in Line 3 and experimented with my indentations of the 'for' lines to no avail.

Can someone tell me what's wrong with my code? I have also tried:

        arcpy.env.workspace = steps
        arcpy.env.overwriteOutput = True
        shplist = arcpy.ListFiles("*footprints.shp")
        tablelist = arcpy.ListFiles("*geotagswBearings.dbf")
        for shp in shplist:
          sprefix = shp.rsplit("*footprints.shp")[0]
             for t in tablelist:
                    tprefix = t.rsplit("*geotagswBearings.dbf")[0]
            if sprefix == tprefix:
                arcpy.AddJoin_management(shp,"PID",t,"PID")

and:

        arcpy.env.workspace = steps
        arcpy.env.overwriteOutput = True
        shplist = arcpy.ListFiles("*footprints.shp")
        tablelist = arcpy.ListFiles("*geotagswBearings.dbf")
        for shp in shplist:
            for t in tablelist:
                    sprefix = shp.rsplit("*footprints.shp")[0]
                    tprefix = t.rsplit("*geotagswBearings.dbf")[0]
                    if sprefix == tprefix:
                        arcpy.AddJoin_management(shp,"PID",t,"PID")

and I've also tried substituting "*footprints.dbf" for "*footprints.shp" as I thought it might need the input_layer table instead of feature class based on the Desktop help section (https://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/add-join.htm)

5
  • 3
    This is more a Python coding logic issue than anything ArcPy specific. Generally speaking, you want to nest your searching.
    – Vince
    May 5, 2021 at 23:20
  • @Vince, I am new to Python. How should I be formatting to do nesting? I've edited my question to show different types of nesting I was trying.
    – BenW
    May 6, 2021 at 3:21
  • 3
    Add Join just adds a join to a layer in memory, it doesn't permanently join the fields to the dataset. If that's what you want to do, use the "Join Fields" tool.
    – user2856
    May 6, 2021 at 3:38
  • Do you have the same number of features classes? A-A B-B, never C-None
    – BERA
    May 6, 2021 at 7:16
  • Thanks all. @BERA yes I do have the same number of feature classes. The issue was four-fold: I needed to use JoinField_management instead of AddJoin as user2856 suggested. I needed to join a table to a table, not a shapefile to a table, I needed to nest looks Vince said. And the sprefix ==prefix was returning FALSE because my rsplit was returning the different file extensions as well. Thanks all.
    – BenW
    May 6, 2021 at 14:24

0

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.

Browse other questions tagged or ask your own question.