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
  • 1
    are all your CSV files in that shpworkspace folder? – Midavalo Jul 28 '16 at 6:21
  • 4
    You need to replace the dash/hyphen (-) in the csv files to underscore (_).Shapefile cannot accept dash/hyphen (-) in the file name. – ahmadhanb Jul 28 '16 at 7:00
  • i have to replace in dahs/hyphen (-) in the csv files to underscore (_) in all 300 csv files which i want make as a input through loop function??? – Shouvik Jha Jul 28 '16 at 7:03
  • 1
    you don't need to replace the - in the csv, just make your python do it when naming the shapefiles – Midavalo Jul 28 '16 at 7:04
  • Sir, please i am not getting your point. Can you tell me the line no where i need to correct . – Shouvik Jha Jul 28 '16 at 7:11
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
  • Thank you sir. after correction the code, in console displayed all the the csv file while running the program but output shp only from single csv. not creating shp from from all csv file. – Shouvik Jha Jul 28 '16 at 7:24
  • Sir , i have attached new one copied from Arc GIS python console. There is no error msg but program consider only single csv to make it shp. I need to convert all csv to shp by single program. – Shouvik Jha Jul 28 '16 at 7:35
  • There is an indentention problem with the arcpy.CopyFeatures line. Align it under the rest of the block – GISGe Jul 28 '16 at 7:42
  • Thank you sir. How i can link through loop all the CSV file to create shp. Kindly help me. – Shouvik Jha Jul 28 '16 at 7:50
  • @ShouvikJha have you re-run your code after correcting the indentation? What happens? – Midavalo Jul 28 '16 at 7:51
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"
  • Thank you. Its printing only single csv file and make it shp file. Not taking others csv file through loop. – Shouvik Jha Jul 28 '16 at 6:34
  • Please edit your question to show us the output from running the latest snippet, and update the snippet to the latest version. – PolyGeo Jul 28 '16 at 6:35
  • Sir, I have attached my input list of csv file and error msg in my question. Kindly help me to solve this task. Thank you – Shouvik Jha Jul 28 '16 at 6:54
  • 1
    Please don't use pictures to show code and output. Many potential answerers (including me) won't look at them unless you include them as text. – PolyGeo Jul 28 '16 at 7:10

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.