Uploading multiple CSV files from folder using code

741
2
03-21-2022 02:22 AM
OriHarash
New Contributor II

I'm trying to read multiple CSV files to my arcGIS pro project.

The csv files consist x,y coordinated so I would like to upload them to the project and show them on the map, as happends when I upload the CSV file through the 'MAP'-->'Add Data'-->'X,Y Point Data'.

I do this action very often so I would like to automate it.

I'll be happy to get some guidance or a script to do so.

 

Tnx a lot

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

There is sample code in the help, but it is for loading 1 file

XY Table To Point (Data Management)—ArcGIS Pro | Documentation

You can right-click on the tool in ArcToolbox and try it in batch mode

xytabletopoint.png

Then if you want a script, you can export to a script and modify it to suit your needs


... sort of retired...
JohannesLindner
MVP Frequent Contributor

When you do it manually, you open the tool XY Table To Point (Data Management)—ArcGIS Pro | Documentation.

To automate the process, you could use a script like this:

csv_files = [
    "H:/test.csv",
    # etc.
    ]

import os
wkid = 25832 # the wkid of your coordinate system
x = "X"
y = "Y"
z = None
gdb = None  # path to the database where you want to save the feature class. None: default gdb of your project
for csv in csv_files:
    print(csv)
    name = os.path.basename(csv).split(".")[0]
    if gdb is not None:
        name = os.path.join(gdb, name)
    arcpy.management.XYTableToPoint(csv, name, x, y, z, wkid)

Have a great day!
Johannes
0 Kudos