<?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: Deleting rows in a txt-document using Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/deleting-rows-in-a-txt-document-using-python/m-p/724032#M56044</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Max,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example how you can remove the beginning lines that contain strings of each text file using the OS and GLOB modules:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os, glob

strings = ("abcdefghijklmnopqrstuvwxyz")
for file in glob.glob(r"C:\temp\python\*.txt"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lines = open(file).readlines()
&amp;nbsp;&amp;nbsp;&amp;nbsp; with open(file) as myFile:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for num, line in enumerate(myFile, 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if any(s in line for s in strings):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val = num
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; open(r'C:\temp\python\newfile.txt', 'w').writelines(lines[val:])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(file)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.rename(r'C:\temp\python\newfile.txt', file)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except IndexError:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 06:58:03 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-12T06:58:03Z</dc:date>
    <item>
      <title>Deleting rows in a txt-document using Python</title>
      <link>https://community.esri.com/t5/python-questions/deleting-rows-in-a-txt-document-using-python/m-p/724031#M56043</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;first of all, sorry for my poor english...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;HERE'S MY PROBLEM:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a txt-file in which XY coordinates are stored.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to load these coordinates using the WARP-tool. So far, this is no problem but in the first row of the txt- document is some text. Unfortunatly the WARP-tool can only read the coordinates, when there is NO text in front of them.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;WHAT I WANT: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;of course I could delete the text manually, but I want to have it done automatically. I think this should be possible because the text is always in the first row(s) of the text-document.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The optimal solution for me would be an Python-Script that says: " 1) load all txt-files in the folder C:\... 2) for each file, delete the first 1 or 2 rows 3) save/overwrite the modified files "&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In&amp;nbsp; the end I want to include the script into the Model Builder...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Max&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 17 Jun 2013 12:03:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/deleting-rows-in-a-txt-document-using-python/m-p/724031#M56043</guid>
      <dc:creator>MaximilianSesselmann</dc:creator>
      <dc:date>2013-06-17T12:03:39Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting rows in a txt-document using Python</title>
      <link>https://community.esri.com/t5/python-questions/deleting-rows-in-a-txt-document-using-python/m-p/724032#M56044</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Max,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an example how you can remove the beginning lines that contain strings of each text file using the OS and GLOB modules:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os, glob

strings = ("abcdefghijklmnopqrstuvwxyz")
for file in glob.glob(r"C:\temp\python\*.txt"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lines = open(file).readlines()
&amp;nbsp;&amp;nbsp;&amp;nbsp; with open(file) as myFile:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for num, line in enumerate(myFile, 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if any(s in line for s in strings):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; val = num
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; open(r'C:\temp\python\newfile.txt', 'w').writelines(lines[val:])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.remove(file)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.rename(r'C:\temp\python\newfile.txt', file)
&amp;nbsp;&amp;nbsp;&amp;nbsp; except IndexError:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 06:58:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/deleting-rows-in-a-txt-document-using-python/m-p/724032#M56044</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-12T06:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting rows in a txt-document using Python</title>
      <link>https://community.esri.com/t5/python-questions/deleting-rows-in-a-txt-document-using-python/m-p/724033#M56045</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, Max.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe this would solve your problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Open the textfile in Excel and then build a macro for the correction in the file. Then you can use the macro for every file you have to correct.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 15 Sep 2013 11:24:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/deleting-rows-in-a-txt-document-using-python/m-p/724033#M56045</guid>
      <dc:creator>BerndGrubert</dc:creator>
      <dc:date>2013-09-15T11:24:32Z</dc:date>
    </item>
  </channel>
</rss>

