<?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 Re: Cut a column, then write to a new file, different suffix in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666746#M51761</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the help.&amp;nbsp; While you were posting this I came up with this that's almost the same. I need to read up on glob. Does arcpy.ListFiles do anything better than glob? Is ListFiles superfluous?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I suppose I'll run the&amp;nbsp; ASCII 3D To Feature Class tool with a new file list and in a new if loop. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import glob import os.path&amp;nbsp; workdir = "G:/elevation/lidar2010/points/asc_grounds/" fileSuffix = ".gnd" outSuffix = ".txt" fileList = glob.glob( workdir+"*"+fileSuffix )&amp;nbsp; for f in fileList: &amp;nbsp; name, ext = os.path.splitext( f ) &amp;nbsp; infile = open( f, "r" )&amp;nbsp; &amp;nbsp; outfile = open( name+outSuffix, "w" ) &amp;nbsp; for line in infile: &amp;nbsp;&amp;nbsp;&amp;nbsp; print &amp;gt;&amp;gt; outfile, " ".join( line.strip().split(" ")[1:4] ) &amp;nbsp; outfile.close() &amp;nbsp; infile.close()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It was hard to import glob into a python window. The window would try to autocorrect it to "import global".&amp;nbsp; I finally figured out I could type in "import glob" and then a space character to over ride it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 25 Oct 2012 15:32:20 GMT</pubDate>
    <dc:creator>PaulHuffman</dc:creator>
    <dc:date>2012-10-25T15:32:20Z</dc:date>
    <item>
      <title>Cut a column, then write to a new file, different suffix</title>
      <link>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666741#M51756</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I need a little hint because I see now I don't understand enough about file input and output in Python.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have some ascii lidar files that need to have the first column in each line removed to get them ready for the XYZI format of the ASCII 3D To Feature Class tool.&amp;nbsp; I wanted to make a list of all the files in a folder with the suffix .gnd and output the modified data to files with the same name but a txt suffix so the original ascii files were not modified.&amp;nbsp; What I got started with was: &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;env.workspace = "G:/elevation/lidar2010/points/asc_grounds" fileSuffix = ".gnd" txtList = arcpy.ListFiles("*" + fileSuffix) &amp;nbsp;&amp;nbsp;&amp;nbsp; for file in txtList: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for line in open("my file"): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parts = line.split(" ") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print " ".join(parts[1:4])&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Then I was stuck. I guess I need a new line inside the first loop to write the file to a new file name with a .txt suffix, and I haven't even figured out how to get the file name stripped of the suffix.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I used to do was I had an XP machine with Unix services for Windows installed that somehow let me use the Linux cut command inside a dos bat file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;for %%f in (*.gnd) do call :sub %%f %%~nf&amp;nbsp; GOTO:EOF&amp;nbsp; :sub cut -d" " -f2-4 %1 &amp;gt; %2.txt&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The DOS %~ can give you a lot of useful file name manipulations.&amp;nbsp; I installed Unix Services for Windows on a new Win 7 64 machine, and I don't understand why, but cut is not available to bat files on the new machine, although I can start a Unix C or K shell where cut is available but the %~n trick is not.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 17:35:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666741#M51756</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2012-10-24T17:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: Cut a column, then write to a new file, different suffix</title>
      <link>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666742#M51757</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;and I haven't even figured out how to get the file name stripped of the suffix.&amp;nbsp; &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You could use a variation of this in your loop to strip the extension off the file name...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

import os

NameMinusExt = os.path.splitext("TheFileName.gnd")[0]

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;## the result would be: TheFileName&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:10:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666742#M51757</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-12T04:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: Cut a column, then write to a new file, different suffix</title>
      <link>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666743#M51758</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Maybe something like this... (completely untested)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
txtList = arcpy.ListFiles("*" + fileSuffix)
for file in txtList:
&amp;nbsp;&amp;nbsp; newFile = os.path.splitext(file)[0] + ".txt"

&amp;nbsp;&amp;nbsp; ###...or maybe?

&amp;nbsp;&amp;nbsp; newFile = str(os.path.splitext(file)[0]) + ".txt"


&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:10:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666743#M51758</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-12T04:10:43Z</dc:date>
    </item>
    <item>
      <title>Re: Cut a column, then write to a new file, different suffix</title>
      <link>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666744#M51759</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This worked really well when I ran it in the Win 7 machine's C shell:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;for f in *.gnd; do cut -d' ' -f2-4 "$f" &amp;gt; "${f%.*}.txt"; done&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;The resulting txt files had unix line endings, but that didn't seem to bother the ASCII 3D To Feature Class tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, it would be nice to figure out how to make this work in python so I could loop through the files, chop off the first column, and run them through the ASCII 3D To Feature Class tool in one pass.&amp;nbsp; I'll keep testing your suggestion.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Oct 2012 21:19:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666744#M51759</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2012-10-24T21:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: Cut a column, then write to a new file, different suffix</title>
      <link>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666745#M51760</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Paul,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try this code - Hopefully it should work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import os

arcpy.env.workspace = r"G:\elevation\lidar2010\points\asc_grounds"
fileSuffix = ".gnd"
fileList = arcpy.ListFiles("*" + fileSuffix)

for f in fileList:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Processing file", f, "..."
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Text File to Write Out
&amp;nbsp;&amp;nbsp;&amp;nbsp; txtFile = os.path.splitext(f)[0] + ".txt"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fobj = open(txtFile, 'w')
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for line in open(f):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; parts = line.split(" ")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fobj.write(" ".join(parts[1:4]))

&amp;nbsp;&amp;nbsp;&amp;nbsp; fobj.close()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Sendhil&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:10:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666745#M51760</guid>
      <dc:creator>SendhilKolandaivel</dc:creator>
      <dc:date>2021-12-12T04:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: Cut a column, then write to a new file, different suffix</title>
      <link>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666746#M51761</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the help.&amp;nbsp; While you were posting this I came up with this that's almost the same. I need to read up on glob. Does arcpy.ListFiles do anything better than glob? Is ListFiles superfluous?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I suppose I'll run the&amp;nbsp; ASCII 3D To Feature Class tool with a new file list and in a new if loop. &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import glob import os.path&amp;nbsp; workdir = "G:/elevation/lidar2010/points/asc_grounds/" fileSuffix = ".gnd" outSuffix = ".txt" fileList = glob.glob( workdir+"*"+fileSuffix )&amp;nbsp; for f in fileList: &amp;nbsp; name, ext = os.path.splitext( f ) &amp;nbsp; infile = open( f, "r" )&amp;nbsp; &amp;nbsp; outfile = open( name+outSuffix, "w" ) &amp;nbsp; for line in infile: &amp;nbsp;&amp;nbsp;&amp;nbsp; print &amp;gt;&amp;gt; outfile, " ".join( line.strip().split(" ")[1:4] ) &amp;nbsp; outfile.close() &amp;nbsp; infile.close()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It was hard to import glob into a python window. The window would try to autocorrect it to "import global".&amp;nbsp; I finally figured out I could type in "import glob" and then a space character to over ride it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Oct 2012 15:32:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/cut-a-column-then-write-to-a-new-file-different/m-p/666746#M51761</guid>
      <dc:creator>PaulHuffman</dc:creator>
      <dc:date>2012-10-25T15:32:20Z</dc:date>
    </item>
  </channel>
</rss>

