Select to view content in your preferred language

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

2494
10
Jump to solution
03-05-2021 02:54 AM
Mick
by
Emerging Contributor

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

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Emeritus

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

That should just about do it....

View solution in original post

0 Kudos
10 Replies
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
Mick
by
Emerging Contributor

There is no comma.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Then a sample of the input would be needed, otherwise you have to read the file, parse it on spaces or tabs and reassemble


... sort of retired...
Mick
by
Emerging Contributor

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?

0 Kudos
DanPatterson
MVP Esteemed Contributor

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.0
4.0 5.0 6.0

as csv file

1.000000, 2.000000, 3.000000
4.000000, 5.000000, 6.000000

you can format as you see fit. 


... sort of retired...
Mick
by
Emerging Contributor

Thank you, can you recommend any extra options with arcpy?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

NumPy has been a standard package in Esri's Python deployment for years.  If your Python deployment has ArcPy, it has NumPy installed too.

0 Kudos
JoeBorgione
MVP Emeritus

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

That should just about do it....
0 Kudos
Mick
by
Emerging Contributor

Can you recommend how to use not only one file to convert but a folder with one hundred files?

0 Kudos