0

I have more than 300 CSV files. Every CSV file contains different Longitude, Latitude and other parameters (e.g. Date, Longitude, Latitude, Elevation, Max Temperature, Min Temperature, Precipitation, Wind, Relative Humidity, Solar Radiation).

I need to convert CSV to shapefile using ArcGIS Python to automatically loop through every CSV. I have made one code but it takes only a single file, and does not loop through every CSV file.

Below I copied my script which is creating only single shapefile taking from last CSV file. Not creating shapefile from all CSV files

    import arcpy,os


... shpworkspace = r"D:\SWAT-WEATHER-DATA2"
... arcpy.env.workspace = shpworkspace
... arcpy.env.overwriteOutput = True
... 
... csvlist = arcpy.ListFiles("*.csv")
... 
... for csvfile in csvlist:
...     print csvfile
...     outlayer = "CSVEventLayer"
...     spatialreference = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
...     arcpy.MakeXYEventLayer_management(csvfile,"Longitude","Latitude",outlayer,spatialreference,"#")
... 
...     shpfile = os.path.splitext(csvfile)[0].replace('-','_')        
... arcpy.CopyFeatures_management(outlayer,shpfile)
... del outlayer
... 
weatherdata-233688.csv
weatherdata-233691.csv
weatherdata-233694.csv
weatherdata-233697.csv
weatherdata-233700.csv
weatherdata-233703.csv
weatherdata-233706.csv
weatherdata-236688.csv
weatherdata-236691.csv
weatherdata-236694.csv
weatherdata-236697.csv
weatherdata-236700.csv
weatherdata-236703.csv
weatherdata-236706.csv
weatherdata-239688.csv
weatherdata-239691.csv
weatherdata-239694.csv
weatherdata-239697.csv
weatherdata-239700.csv
weatherdata-239703.csv
weatherdata-239706.csv
0

2 Answers 2

2

As @ahmadhanb said in a comment, you need to replace the hyphen - with an underscore _ for the shapefile.

    # Get shapefile name and replace hyphen with underscore
    shpfile = os.path.splitext(csvfile)[0].replace('-','_')        
    arcpy.CopyFeatures_management(outlayer,shpfile)

In your updated question you have re-added your code which has an indentation issue. The reason you only get one shp is because your copy features is not indented as part of the for loop.

for csvfile in csvlist:
    print csvfile
    outlayer = "CSVEventLayer"
    spatialreference = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
    arcpy.MakeXYEventLayer_management(csvfile,"Longitude","Latitude",outlayer,spatialreference,"#")

    shpfile = os.path.splitext(csvfile)[0].replace('-','_')        
    arcpy.CopyFeatures_management(outlayer,shpfile)

    del outlayer
0
1

Your code appears to be looping through all the *.csv files in the D:\SWAT-WEATHER-DATA folder, which you could confirm by including some print statements. Use print statements any where you need to be certain that variables are set to what you think they are.

Instead of:

csvlist = arcpy.ListFiles("*.csv")
for csvfile in csvlist:
    outlayer = "CSVEventLayer"

use:

csvlist = arcpy.ListFiles("*.csv")
print csvlist
for csvfile in csvlist:
    print csvfile
    outlayer = "CSVEventLayer"
0

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.