Python questions (How covert txt files to .csv files?)

1698
10
Jump to solution
03-05-2021 02:54 AM
Mick
by
New Contributor

My company asks to convert txt files to .csv files. Could you advise how to write a Python script for ArcGIS?  

0 Kudos
10 Replies
JoeBorgione
MVP Emeritus

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....

That should just about do it....
0 Kudos