I have a script that I've used to import information in a CSV to a shapefile.
What I'd like to do is make it so that this script will run on all CSVs in a specific folder location, creating multiple shapefiles.
This is a bit beyond my current programming skills. Can someone point me in the right direction? The current script that imports from a single CSV is below.
import arcpy
import csv
import os
print "arcpy imported"
"""
# Author: John K. Tran
# Contact: jtran20@masonlive.gmu.edu
Input CSV can look like:
Lat,Lon,First Name,Last Name
12.34,56.78,Joe,Smith
0.98,7.65,Jane,Doe
65.43,43.21,Bob,Sagat
Make sure 'Lat' and 'Lon' fields are the 1st and 2nd column in the CSV
respectively.
"""
incsv = arcpy.GetParameterAsText(0) # Change this to the path of your CSV file.
outfc = arcpy.GetParameterAsText(1) + "\\" + arcpy.GetParameterAsText(2) + ".shp" # Change this to the path of your output FC.
spatialref = arcpy.SpatialReference(4326) # Create the spatial reference object as WGS84. Can modify if desired.
if not arcpy.Exists(outfc): # Create the output feature class if needed. (if this is not an existing path, create it)
arcpy.CreateFeatureclass_management(os.path.dirname(outfc),
os.path.basename(outfc), "POINT", None, None, None, spatialref)
csv.register_dialect("xls", delimiter=",", lineterminator="\n") # Register the dialect for native CSV syntax in Microsoft Excelself.
f = open(incsv, "r")
reader = csv.reader(f, dialect = "xls")
headers = reader.next() # Read the first line as the header names.
for header in headers[2:]: # Add fields for remaining columns if needed. Default is TEXT field.
arcpy.AddField_management(outfc, header, "TEXT")
cursor = arcpy.da.InsertCursor(outfc, ['SHAPE@XY'] + headers[2:]) # Create
InsertCursor.
count = 0
for row in reader:
if count % 1000 == 0:
print "processing row {0}".format(count)
Ycoord = row[0] # Make sure 'Lat' is in the 1st column.
Xcoord = row[1] # Make sure 'Lon' is in the 2nd column.
newrow = [(float(Xcoord), float(Ycoord))] + row[2:]
cursor.insertRow(newrow) # Insert point in FC for each row in CSV.
count += 1
del cursor
f.close()