I have a problem I'm trying to solve. I want to join serval CSV files to a feature class, I would like to create one new feature class for each CSV files. If it's possible each fc would be named after the CSV file used.

For example if I have: abc.csv and zxy.csv I want to feature class to be named , with each fc corresponding to one of the csv file, abc and zxy.

inFeatures = r"path to my feature class"
layerName = "fc_layer"
joinTables = r"path to the folder where my CSV files are"
joinFieldlyr = "CI_INT"
joinFieldtab = "CI"
outFeature = r"path to the folder dataset where I want my feature class"

arcpy.MakeFeatureLayer_management(inFeatures, layerName)
arcpy.AddJoin_management(layerName, joinFieldlyr, joinTables, joinFieldtab)
arcpy.CopyFeatures_management(layerName, outFeature)

That code work perfectly for one file but I can't get the name part right. I tried to use os.path.join and os.path.splitext so I can find and use the name of my files but without any succes. I also tried to code a loop to iterate trough my folder so I don't have to do it manually.

  • So you have a feature class and want to join csv1 and then export, remove join and join csv2 and export etc..? Did it work the first time without creating a table view of the csv? – BERA May 12 '17 at 13:08

Try this:

import arcpy, os

#Change to match your data

#Find all csv files in csvfolder and add to csvlist
for file in os.listdir(csvfolder):
    if file.endswith(".csv"):
        csvlist.append(os.path.join(csvfolder, file))

#For each csv in csvlist join and export
for csv in csvlist:
    arcpy.MakeFeatureLayer_management(in_features=inFeatures, out_layer='fclyr')
    arcpy.MakeTableView_management(in_table=csv, out_view='csvview')
    arcpy.AddJoin_management(in_layer_or_view='fclyr', in_field='CI_INT', join_table='csvview', join_field='CI')

If you have whitespaces in the csv name you can use str.replace() to remove them. Last line of code will then be:

arcpy.CopyFeatures_management(in_features='fclyr',out_feature_class=os.path.splitext(os.path.basename(csv))[0].replace(" ","")
  • 1
    What if you're using forward slashes? Use the module designed for path manipulation: os.path.splitext(os.path.basename(csv))[0] – Paul May 12 '17 at 19:59
  • What I don't understand is that if do print os.path.splitext(os.path.basename(csv))[0] I get the correct name but in the loop it ends up not working. – Victor A May 15 '17 at 8:15
  • Thank you for your answers (@Paul @Bera) but your code didn't work due to the way my csv files are named. The loop works for the GIS stuff but not for the rest. I think part of the problem is that my files are full of white-spaces. For example I have one file named "ABC Region1 City 1" and "ABC Region1 City 2" and the script ends up giving me an error message "arcgisscripting.ExecuteError: ERROR 000210: Cannot create output 'C:\filegeodatabase.gdb'\ABC Region1 City 2 Failed to execute (CopyFeatures)." – Victor A May 15 '17 at 8:16
  • @VictorA can't use spaces in FGDB. Use ValidateTableName. – Paul May 15 '17 at 15:38
  • @Paul: Your way is better, i have changed the code! – BERA May 16 '17 at 9:02

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.