Python: Join Table to Shapefile and export it to GDB

10246
2
Jump to solution
05-21-2015 01:35 AM
PV
by
New Contributor III

Hi
I have many tables which I need to join to a Shapefile. Each table has to be joined to the shapefile seperately and then must be exported into a file GDB.
Anyone knows how to do so with python? Is there a way that the code runs automatically through each table in the folder and joins it to the shapefile, exports it as FC in the GDB, gives it the file name of the table, then removes the join and joins the next table to the same shapefile? I work with ArcGIS 10.1

I came across this site ArcGIS Help 10.1 and also ArcGIS Help 10.1  but I have no clue how to get started to be honest..

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi P V,

Here is an example on how you could do this:

import arcpy

from arcpy import env

env.overwriteOutput = 1

env.workspace = r"C:\temp\tables"

shapefile = r"C:\data\parcels.shp"

#loopo through tables in C:\temp\tables

for table in arcpy.ListTables("*"):

  #Create a new feature layer, this will remove previous join

  arcpy.MakeFeatureLayer_management(shapefile, "parcelLyr")

  #Join table to feature layer

  arcpy.AddJoin_management("parcelLyr", "PIN", table, "PIN")

  #Retrieve table name

  tableName = table.split(".")[0]

  #Export joined layer to new feature clas

  arcpy.FeatureClassToFeatureClass_conversion("parcelLyr", r"C:\data\ParcelData.gdb", tableName)

View solution in original post

2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi P V,

Here is an example on how you could do this:

import arcpy

from arcpy import env

env.overwriteOutput = 1

env.workspace = r"C:\temp\tables"

shapefile = r"C:\data\parcels.shp"

#loopo through tables in C:\temp\tables

for table in arcpy.ListTables("*"):

  #Create a new feature layer, this will remove previous join

  arcpy.MakeFeatureLayer_management(shapefile, "parcelLyr")

  #Join table to feature layer

  arcpy.AddJoin_management("parcelLyr", "PIN", table, "PIN")

  #Retrieve table name

  tableName = table.split(".")[0]

  #Export joined layer to new feature clas

  arcpy.FeatureClassToFeatureClass_conversion("parcelLyr", r"C:\data\ParcelData.gdb", tableName)

View solution in original post

PV
by
New Contributor III

Thank you a lot! This code worked PERFECTLY!!!
I only added this line to it: env.qualifiedFieldNames = False

0 Kudos