I have a geodatabase feature class file and I want to join the feature class file to CSV files respectively.

For example:

Join the geodatabase feature class and CSV 1 => save as CSV 1 in gdb.

Join the geodatabase feature calss and CSV 2 => save as CSV 2 in gdb.

.... And so on until CSV 50.

Here is my code:

# Import modules and set up
import arcpy
import os

arcpy.env.overwriteOutput = True
csvfolder = "X:/SDEmanagement/imports/ACS2017/Final May 16/Test Export/"

#List csv files
for file in os.listdir(csvfolder):
    if file.endswith(".csv"):
        csvlist.append(os.path.join(csvfolder, file))

#The geodatabase feature class file to join
allgeo = "X:/SDEmanagement/imports/GEOID_joinprep.gdb/AllGeo"

#Geodatabase location to save the joined files
gdb ="X:/SDEmanagement/imports/JoinedGeoTable_topic.gdb/"

#Loop join - CSV files and All Geo file respectively.
for csv in csvlist:
    arcpy.MakeFeatureLayer_management(allgeo, os.path.splitext(os.path.basename(csv))[0]+'_lyr')
    arcpy.MakeTableView_management(csv, os.path.splitext(os.path.basename(csv))[0]+'_csv')
    arcpy.TableToGeodatabase_conversion(os.path.splitext(os.path.basename(csv))[0]+'_csv', gdb)
    arcpy.AddJoin_management(os.path.splitext(os.path.basename(csv))[0]+'_lyr','GEOID', gdb+os.path.splitext(os.path.basename(csv))[0]+'_csv', 'GEOID')
    arcpy.CopyFeatures_management(os.path.splitext(os.path.basename(csv))[0]+'_lyr', gdb+os.path.splitext(os.path.basename(csv))[0])

I got an error as below on the second or third iteration of the loop:

Error - Failed to convert Social_EdAttainment_csv. ERROR 001156: Failed on input OID -1, could not write value '13001950100' to output field GEOID Failed to execute (CopyRows).

The error occurs in the TableToGeodatabase Function in the iteration.

Strangely, when I move the second file which gave me an error before to be in the first item in the list which makes the file to go into the loop in the first iteration, it works. So I guess the error does not come from the file itself. I just guess this is related to join in-memory files.

I tried to change personal geodatabase to file geodatabase but I got the same error.

I tried JoinField but also got the same error.

  • 2
    Geodatabase feature classes are tables, not files. You can't place a comma-separated value file in a geodatabase, only create a table based on a CSV data source. Personal geodatabase (Access) has been superseded by file geodatabase in function and capabilities. Your error appears to be due to an integer value exceeding long (32-bit) integer range. – Vince Jun 5 '19 at 14:44
  • You have to check if your csv files do not contain fields with an unauthorized length.
    • Also, you have to make sure that your both elements (OS. path. splitext (OS. path. basename (csv)) [0] +'_lyr' and OS. path. splitext (OS. path. basename (csv)) [0] +'_csv') have the same field type and the same length concerning your field 'GEOID'.
| improve this answer | |
  • Thanks for your comment. I added 'Alter Field' function to match the field property before I do add join: arcpy.AlterField_management(os.path.splitext(os.path.basename(csv))[0]+'_T', field, field, field, dtype, dlength) – Summer S Jun 5 '19 at 18:19

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.