4

I'm trying to join a shapefile to a csv file. The join works but the attribute table has the long name of the shapefiles which makes the fields not readable.

How can i fix this problem?

Calling the scrip from an os window as

C:/Python27/ArcGIS10.4/python csv_2_dbf_tdm.py "D:/01_Inputs/y2035/cube_se2035.csv" "no_bld_tdm_draft1"

where cube_se2035 is sys.argv[1] and the no_b;dm_tdm_draf1 is a folder name

import arcpy
import sys
import os

basename =  os.path.basename(sys.argv[1]).replace(".csv", ".dbf")
dbf_path = sys.argv[1].replace(".csv", ".dbf")
se_shp = os.path.join(os.getcwd(), "data\se_folder_shapefile\se919.shp")
sys.argv[1] = se_path = "D:/cube_se{year}.csv".format(year=2035)

shapefileName = "temp" 
arcpy.MakeFeatureLayer_management(se_shp,shapefileName)

###Join the feature layer to a table
arcpy.AddJoin_management(shapefileName, "DASZ2010",dbf_path, "dasz_id") 

### Copy the layer to a new permanent feature class

arcpy.CopyFeatures_management(dd, os.path.join(cube_folder, basename.strip(".dbf")))

Here is the table after the join. All the fields have the source name and I just need the field name without the source.

enter image description here

  • Before your addjoin command, try adding in this command: arcpy.env.qualifiedFieldNames = False – KHibma Feb 11 at 19:35
  • As to your performance problem, try reading your CSV into an in_memory table (Table to Table resources.arcgis.com/en/help/main/10.2/index.html#//… should work if you don't want to do the import using primitive read line) then using Join Field resources.arcgis.com/en/help/main/10.2/index.html#//… instead of copy features from the joined layer. CSV files are notably clunky and problematic, I would suggest always to convert to a more rigid table format before using, both for performance and error avoidance (trap bad values early). – Michael Stimson Feb 12 at 4:45
6

Turn off Qualified Field Names before joining.

arcpy.env.qualifiedFieldNames = False

Tools that honor the Qualified Field Names environment use this setting to distinguish between qualified or unqualified field names. Qualified field names are the names of fields in a feature class or table that have the name of the origin feature class or table appended onto the field name. This setting is relevant when working with joined data.

  • it seems that it should be an argument of the function. Would be easier to know about it. – Daniel Feb 13 at 23:09

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.