0

I have hundreds of CSV files with the naming convention similar to:

prcp_025_01_01.csv
prcp_025_05_01.csv
prcp_025_08_01.csv
prcp_050_01_01.csv
prcp_050_05_01.csv
prcp_050_08_01.csv
prcp_025_01_15.csv
prcp_025_05_15.csv
prcp_025_08_15.csv

I was wanting to create a shapefile for each CSV like so:

prcp_025_01_01.shp
prcp_025_05_01.shp
prcp_025_08_01.shp
prcp_050_01_01.shp
prcp_050_05_01.shp
prcp_050_08_01.shp
prcp_025_01_15.shp
prcp_025_05_15.shp
prcp_025_08_15.shp

When running the following code, it will create the first shapefile of prcp_025_01_01.shp But then error out with

ERROR 000725: Layer Name or Table View: Dataset prcp_dly_layer already exists. Failed to execute (MakeXYEventLayer).

Im running 10.5.1 Below is my script. Any suggestions on how to fix it?

import arcpy
import os
arcpy.env.workspace=r"C:\path"
shps=arcpy.ListFeatureClasses()
SR=arcpy.SpatialReference(4269) #WGS 1984
for shp in shps:
 print arcpy.Describe(shp).spatialReference.name
elem = ["025", "050", "100", "400"]
month = ["01", "05", "08", "12"]
day = ["01", "15", "31"]
for element in elem:
 for mon in month:
 for dy in day:
 in_table=r"C:\path\csv_files/prcp_" + element + "_" + mon + "_" + dy + ".csv"
 prcp_dly_layer=arcpy.MakeXYEventLayer_management(in_table, "LON", "LAT", "prcp_dly_layer", SR)
 
 fc=arcpy.CopyFeatures_management(prcp_dly_layer, r"C:\path\prcp/" + "prcp_" + element + "_" + mon + "_" + dy + "_" + '.shp')
print(arcpy.GetCount_management(prcp_dly_layer))
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 9, 2021 at 20:37
1
  • 2
    Just like the message says, the "prcp_dly_layer" layer exists. You either need to sequence it or delete the layer before creating it a second time. Commented Mar 9, 2021 at 21:30

2 Answers 2

1

Based on what you've stated you want as output I would try this simpler code instead. Note that the output location for the shapefiles is currently set as the folder containing the CSVs outPath = arcpy.env.workspace, so you may want to change this.

#Import modules
import arcpy
import os
#Set workspace
arcpy.env.workspace = r"C:\path"
#Create list of all csvs in folder
inputFiles = arcpy.ListFiles("*.csv")
#Variable
outPath = arcpy.env.workspace
#Loop through each csv
for file in inputFiles:
 #Set the output name to be the same as the input
 outLayer = file
 #Create the temporary XY layer
 arcpy.MakeXYEventLayer_management(file, "LON", "LAT", outLayer)
 
 #Make the layer a permanent shapefile
 arcpy.FeatureClassToFeatureClass_conversion(outLayer, outPath, outLayer)
answered Mar 10, 2021 at 10:34
2
  • Thanks for the simpler code, however its not creating anything for me. Id really like to have this working for me as I have hundreds of csv's with random names that need to be created into shapefiles. I notice it does not declare a projection (WGS 1984) or have a print statement at the end. However I do not think those are the issues here. Commented Mar 24, 2021 at 10:46
  • You say it's not creating anything, at what stage - creating XY layer or shapefile? Are you getting any errors? Commented Mar 24, 2021 at 15:52
0

I was able to figure it out. Below is a snippet of my final code.

`SR=arcpy.SpatialReference(4269) #WGS 1984
 inputFiles = arcpy.ListFiles("*.csv")
 outPath = arcpy.env.workspace
 for csvfile in inputFiles:
 print csvfile
 #Set the output name to be the same as the input
 outLayer = "CSVEventLayer"
 arcpy.MakeXYEventLayer_management(csvfile,"LON","LAT",outLayer,SR)
 shpfile = os.path.splitext(csvfile)[0].replace('csv','')
 arcpy.CopyFeatures_management(outLayer,shpfile)
 arcpy.Delete_management(outLayer)`
answered Mar 24, 2021 at 16:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.