<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Python questions (How covert txt files to .csv files?) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033315#M60224</link>
    <description>&lt;P&gt;My company asks to convert txt files to .csv files. Could you advise how to write a Python script&amp;nbsp;&lt;SPAN&gt;for ArcGIS?&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Mar 2021 10:54:38 GMT</pubDate>
    <dc:creator>Mick</dc:creator>
    <dc:date>2021-03-05T10:54:38Z</dc:date>
    <item>
      <title>Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033315#M60224</link>
      <description>&lt;P&gt;My company asks to convert txt files to .csv files. Could you advise how to write a Python script&amp;nbsp;&lt;SPAN&gt;for ArcGIS?&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 10:54:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033315#M60224</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-05T10:54:38Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033317#M60225</link>
      <description>&lt;P&gt;The bigger question is the *.txt files in comma separated values format?&lt;/P&gt;&lt;P&gt;Otherwise it would be a simple renaming operation.&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;A previous answer provides the solution, which just needs to be put within a loop&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/using-python-to-split-and-rename-file/m-p/705307#M54583" target="_blank"&gt;Solved: Using Python to split and rename file - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 10:59:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033317#M60225</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-05T10:59:25Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033319#M60226</link>
      <description>&lt;P&gt;There is no comma.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 10:59:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033319#M60226</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-05T10:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033320#M60227</link>
      <description>&lt;P&gt;Then a sample of the input would be needed, otherwise you have to read the file, parse it on spaces or tabs and reassemble&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 11:00:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033320#M60227</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-05T11:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033321#M60228</link>
      <description>&lt;P&gt;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?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 11:08:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033321#M60228</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-05T11:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033333#M60230</link>
      <description>&lt;P&gt;I use numpy&lt;/P&gt;&lt;LI-CODE lang="python"&gt;fname = r"c:\temp\space.txt"
a = np.loadtxt(fname, delimiter=" ")

outname = fname[:-3] + "csv"
np.savetxt(outname, a, fmt='%f', delimiter=", ")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;a text file&lt;/P&gt;&lt;P&gt;1.0 2.0 3.0&lt;BR /&gt;4.0 5.0 6.0&lt;/P&gt;&lt;P&gt;as csv file&lt;/P&gt;&lt;P&gt;1.000000, 2.000000, 3.000000&lt;BR /&gt;4.000000, 5.000000, 6.000000&lt;/P&gt;&lt;P&gt;you can format as you see fit.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 11:49:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033333#M60230</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-05T11:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033342#M60232</link>
      <description>&lt;P&gt;Thank you, can you recommend any extra options with&amp;nbsp;arcpy?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 12:36:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033342#M60232</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-05T12:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033453#M60238</link>
      <description>&lt;P&gt;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:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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()&lt;/LI-CODE&gt;&lt;P&gt;Assuming you have the file C:\temp\Coords.txt that looks like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;-111.45  40.57
-112.05  40.06&lt;/LI-CODE&gt;&lt;P&gt;I stole this right from&amp;nbsp;&lt;A href="https://www.kite.com/python/answers/how-to-update-and-replace-text-in-a-file-in-python" target="_blank"&gt;https://www.kite.com/python/answers/how-to-update-and-replace-text-in-a-file-in-python&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 16:36:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033453#M60238</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-03-05T16:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033456#M60239</link>
      <description>&lt;P&gt;NumPy has been a standard package in Esri's Python deployment for years.&amp;nbsp; If your Python deployment has ArcPy, it has NumPy installed too.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 16:41:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033456#M60239</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-03-05T16:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033671#M60250</link>
      <description>&lt;P&gt;Can you recommend how to use not only one file to convert but a folder with one hundred files?&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 05:06:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033671#M60250</guid>
      <dc:creator>Mick</dc:creator>
      <dc:date>2021-03-06T05:06:04Z</dc:date>
    </item>
    <item>
      <title>Re: Python questions (How covert txt files to .csv files?)</title>
      <link>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033710#M60263</link>
      <description>&lt;P&gt;You'll want to get familiar with the &lt;A href="https://careerkarma.com/blog/python-list-files-in-directory/" target="_self"&gt;os module in python,&lt;/A&gt; and especially the os.listdir() method.&amp;nbsp; Once you get a list of the file names, you &lt;A href="https://www.geeksforgeeks.org/iterate-over-a-list-in-python/" target="_self"&gt;can iterate through that list&lt;/A&gt; and apply the code I provided above upon each iteration.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Finally you'll want to rename your .txt files to .csv files.&amp;nbsp; 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.&amp;nbsp; &lt;A href="https://www.w3schools.com/python/python_file_handling.asp" target="_self"&gt;This page is worthy of a bookmark&lt;/A&gt; 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.&lt;/P&gt;&lt;P&gt;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.&amp;nbsp; 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.&amp;nbsp; I'm going to take advantage of that and pull my waders and tie on a &lt;A href="https://www.google.com/search?q=griffiths+gnat+pattern&amp;amp;rlz=1C1GCEB_enUS895US895&amp;amp;sxsrf=ALeKk03EAhX2fXJ6j-9S1Fdk4LUlsIaMyg:1615042104920&amp;amp;source=lnms&amp;amp;tbm=isch&amp;amp;sa=X&amp;amp;ved=2ahUKEwiczdfX9JvvAhVFLs0KHYBEAcAQ_AUoAnoECBcQBA&amp;amp;biw=1920&amp;amp;bih=937" target="_self"&gt;Griffiths Gnat&lt;/A&gt;&amp;nbsp;with some sort of &lt;A href="https://www.google.com/search?q=midge+emerger+flies&amp;amp;tbm=isch&amp;amp;ved=2ahUKEwio7Krb9JvvAhXsAp0JHYPCD_sQ2-cCegQIABAA&amp;amp;oq=midge+&amp;amp;gs_lcp=CgNpbWcQARgAMgQIIxAnMgQIABBDMgQIABBDMgQIABBDMgQIABBDMgQIABBDMgUIABCxAzIFCAAQsQMyAggAMgIIADoHCAAQsQMQQ1CjnAVY3qUFYPOyBWgAcAB4AIABVYgB0AOSAQE2mAEAoAEBqgELZ3dzLXdpei1pbWfAAQE&amp;amp;sclient=img&amp;amp;ei=QJZDYKjMIuyF9PwPg4W_2A8&amp;amp;bih=937&amp;amp;biw=1920&amp;amp;rlz=1C1GCEB_enUS895US895" target="_self"&gt;Midge emerger&lt;/A&gt; as a &lt;A href="https://www.google.com/search?bih=937&amp;amp;biw=1920&amp;amp;rlz=1C1GCEB_enUS895US895&amp;amp;hl=en&amp;amp;sxsrf=ALeKk02J0fcAL6tHtxk0F4UZiLYvWOQRIA%3A1615042447172&amp;amp;ei=j5dDYNX-CdG6tAbk4JqIBw&amp;amp;q=dry+fly+dropper+rig&amp;amp;oq=dry+fly+dr&amp;amp;gs_lcp=Cgdnd3Mtd2l6EAEYATICCAAyAggAMgIIADICCC4yAggAMgYIABAWEB4yBggAEBYQHjIGCAAQFhAeMgYIABAWEB4yBggAEBYQHjoNCC4QxwEQrwEQJxCTAjoECCMQJzoFCC4QkQI6BAgAEEM6BQgAEJECOggILhDHARCjAjoHCCMQ6gIQJzoECC4QQzoFCAAQyQM6BQgAEJIDOggILhDHARCvAToHCAAQyQMQQ1DrEVjgPmC9UWgCcAJ4AIABaYgByweSAQQxMC4xmAEAoAEBqgEHZ3dzLXdperABCsABAQ&amp;amp;sclient=gws-wiz" target="_self"&gt;dropper&lt;/A&gt;....&lt;/P&gt;</description>
      <pubDate>Sat, 06 Mar 2021 14:56:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-questions-how-covert-txt-files-to-csv-files/m-p/1033710#M60263</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2021-03-06T14:56:32Z</dc:date>
    </item>
  </channel>
</rss>

