2

I have a shapefile with an attribute table like this:

FID   Shape      ID
0     Polgyon    3 
1     Polgyon    3
2     Polygon    3
3     Polygon    3

and numerous csv files which look something like this:

FID   p-value  t-stat
0     0.50     0.0004
1     0.01     0.0006
2     0.02     0.0007
3     0.04     0.0008

and I am joining the csv files to the shapefile and then exporting like this:

import arcpy, os
arcpy.CheckOutExtension('Spatial')

arcpy.env.workspace=r'H:\path_to_csv_files'
shapefile=r'H:\pathway_to_shapefile.shp'
out=r'H:\output_pathway'
#loop through csv files  
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", "FID", table, "FID", 'KEEP_COMMON') 
  tableName = table.split(".")[0] 
  if not os.path.isdir(out):
            os.mkdir(out)
#   #Export joined layer to new feature clas  
  arcpy.FeatureClassToFeatureClass_conversion("parcelLyr", out, tableName)

but my out put table looks like this:

FID   Shape      ID  NDVI__5_cs   NDVI__5__1
0     Polgyon    3   0.50         0.0004
1     Polgyon    3   0.01         0.0006
2     Polygon    3   0.02         0.0007 
3     Polygon    3   0.04         0.0008

NDVI_5.csv is the name of my first csv file so it is for some reason putting the name of the csv file as the column headers after the join. When I do the join manually in arcgis this doesn't happen though.

4
  • 1
    Try JoinField resources.arcgis.com/en/help/main/10.2/index.html#//…, it does everything your script does in one line. You might want to edit your column headers, dashes in field names are not allowed. Jul 7, 2016 at 21:55
  • I will look into it, I removed all the dashes and the same problem is still there. Jul 7, 2016 at 22:23
  • Also the Fully Qualified Field Names setting resources.arcgis.com/en/help/main/10.2/index.html#//… set to False may help in your join, it should prevent the join name being used in the field name (i.e. p_value instead of NDVI_5_CSV.p_value) which is being shortened to the maximum field name length for a shape file by FeatureClassToFeatureClass. Jul 8, 2016 at 0:06
  • @MichaelMiles-Stimson It looks like enough in your two answers to write a quick answer.
    – PolyGeo
    Jul 15, 2016 at 7:54

1 Answer 1

1

As commented by @MichaelMiles-Stimson:

Try JoinField http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000065000000, it does everything your script does in one line. You might want to edit your column headers, dashes in field names are not allowed.

and:

Also the Fully Qualified Field Names setting http://resources.arcgis.com/en/help/main/10.2/index.html#//001w00000008000000 set to False may help in your join, it should prevent the join name being used in the field name (i.e. p_value instead of NDVI_5_CSV.p_value) which is being shortened to the maximum field name length for a shape file by FeatureClassToFeatureClass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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