My company asks to convert txt files to .csv files. Could you advise how to write a Python script for ArcGIS?
You won't use arcpy as it is nothing more than a python module but you can use the re (regular expression) module to do it:
import re myFile = r'C:\temp\Coords.txt' with open(myFile,'r+') as f: text = f.read() text = re.sub(' ',',',text) f.seek(0) f.write(text) f.truncate() f.close()
Assuming you have the file C:\temp\Coords.txt that looks like this:
-111.45 40.57 -112.05 40.06
I stole this right from https://www.kite.com/python/answers/how-to-update-and-replace-text-in-a-file-in-python
You'll want to get familiar with the os module in python, and especially the os.listdir() method. Once you get a list of the file names, you can iterate through that list and apply the code I provided above upon each iteration.
Finally you'll want to rename your .txt files to .csv files. You have a couple options here; you can follow the code above that edits the contents of the .txt files and then using the os module re-name them, or you edit those files and write the edits to a different file. This page is worthy of a bookmark for how to open, read, write, append etc files. You may want to copy a few of your files and work on them to iron out the kinks before you tackle all one hundred.
I've purposely left out specific code here, as well as provided a number of different online Python resources for you as I'm a firm believer in the 'Teach a man to fish' philosophy. And speaking of which, it's Saturday, and the temperatures are expected to be near 60 here in Utah, which means there is a good chance of a midge hatch on local rivers. I'm going to take advantage of that and pull my waders and tie on a Griffiths Gnat with some sort of Midge emerger as a dropper....
Can you recommend how to use not only one file to convert but a folder with one hundred files?
NumPy has been a standard package in Esri's Python deployment for years. If your Python deployment has ArcPy, it has NumPy installed too.
Thank you, can you recommend any extra options with arcpy?
I use numpy
fname = r"c:\temp\space.txt" a = np.loadtxt(fname, delimiter=" ") outname = fname[:-3] + "csv" np.savetxt(outname, a, fmt='%f', delimiter=", ")
a text file
1.0 2.0 3.04.0 5.0 6.0
as csv file
1.000000, 2.000000, 3.0000004.000000, 5.000000, 6.000000
you can format as you see fit.
I have only two spaces between coordinates in my files. Can you recommend a code to replace two spaces for a comma in the script?
Then a sample of the input would be needed, otherwise you have to read the file, parse it on spaces or tabs and reassemble
There is no comma.
The bigger question is the *.txt files in comma separated values format?
Otherwise it would be a simple renaming operation.
And in any event, the filename extension doesn't need to be *.csv for it to be read as one, it is the contents that are important
A previous answer provides the solution, which just needs to be put within a loop
Solved: Using Python to split and rename file - Esri Community
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.