<?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: Parsing text file and output to text file or csv in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22454#M1728</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;well there are no commas for the csv (comma, separated values) reader to separate on.&amp;nbsp; Can you not just parse this using spaces?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 20 Jun 2016 15:51:28 GMT</pubDate>
    <dc:creator>DanPatterson_Retired</dc:creator>
    <dc:date>2016-06-20T15:51:28Z</dc:date>
    <item>
      <title>Parsing text file and output to text file or csv</title>
      <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22453#M1727</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I have a gps text file that has coordinates with other variables. I need to parse the file in order to get Lat, Long and Altitude values from the file and write it into new text file or csv. I tried to read the lines but I got error like this: &lt;/P&gt;&lt;P&gt;&lt;IMG class="image-1 jive-image" src="https://community.esri.com/legacyfs/online/208333_pastedImage_0.png" style="max-width: 1200px; max-height: 900px;" /&gt;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# This script reads a GPS data and prints a list of coordinate pairs&lt;/P&gt;&lt;P&gt;import csv&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Set up input &lt;/P&gt;&lt;P&gt;gpsdata = open("C:/testdata/telemetry2.txt", "r")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Set and process CSV reader &lt;/P&gt;&lt;P&gt;csvReader = csv.reader(gpsdata)&lt;/P&gt;&lt;P&gt;header = csvReader.next()&lt;/P&gt;&lt;P&gt;latvalueIndex = header.index("lat")&lt;/P&gt;&lt;P&gt;lonvalueIndex = header.index("long")&lt;/P&gt;&lt;P&gt;altvalueIndex = header.index("alt")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Make an empty list&lt;/P&gt;&lt;P&gt;coordList = []&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Loop through the lines in the file and get each coordinate&lt;/P&gt;&lt;P&gt;for row in csvReader:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lat = row[latvalueIndex]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lon = row[lonvalueIndex]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; alt = row[altvalueIndex]&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; coordList.append([lat,lon,alt])&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Print the coordinate list&lt;/P&gt;&lt;P&gt;print coordList&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have also attached the data sample. I will appreciate for any help to get this fixed. Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Jun 2016 15:46:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22453#M1727</guid>
      <dc:creator>KushendraShah1</dc:creator>
      <dc:date>2016-06-20T15:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing text file and output to text file or csv</title>
      <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22454#M1728</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;well there are no commas for the csv (comma, separated values) reader to separate on.&amp;nbsp; Can you not just parse this using spaces?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Jun 2016 15:51:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22454#M1728</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-06-20T15:51:28Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing text file and output to text file or csv</title>
      <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22455#M1729</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dan, the data is tab delimited, the script works when you insert commas between columns. I was thinking if there is solution that directly read and write tab delimited data or like this type. Thanks&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Jun 2016 16:08:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22455#M1729</guid>
      <dc:creator>KushendraShah1</dc:creator>
      <dc:date>2016-06-20T16:08:28Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing text file and output to text file or csv</title>
      <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22456#M1730</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;well a spreadsheet can, but I presume you tried that already, otherwise you will have to parse on the tabs&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Jun 2016 16:25:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22456#M1730</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-06-20T16:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing text file and output to text file or csv</title>
      <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22457#M1731</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Do you have NotePad++&amp;nbsp;&amp;nbsp; (free &lt;A href="https://notepad-plus-plus.org/download/v6.8.2.html" title="https://notepad-plus-plus.org/download/v6.8.2.html"&gt;Notepad++ v6.8.2&lt;/A&gt; )&lt;/P&gt;&lt;P&gt;You can easily replace the tabs with commas with that.&amp;nbsp; There are also ways to do it with Word (a tab is ^t I believe). &lt;/P&gt;&lt;P&gt;&lt;IMG __jive_id="208323" alt="" class="image-1 jive-image" height="448" src="https://community.esri.com/legacyfs/online/208323_pastedImage_2.png" style="width: 569px; height: 448.776px;" width="569" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Or in python&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14664405107911909 jive_text_macro" data-renderedposition="574.3499755859375_8_907_16" jivemacro_uid="_14664405107911909"&gt;&lt;P&gt;aRecord.replace("\t", ",")&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Jun 2016 16:33:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22457#M1731</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2016-06-20T16:33:20Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing text file and output to text file or csv</title>
      <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22458#M1732</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Would this be of any help? ..... I have use this for tab delimited files (excel style).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.python.org/2/library/csv.html" title="https://docs.python.org/2/library/csv.html" rel="nofollow noopener noreferrer" target="_blank"&gt;13.1. csv — CSV File Reading and Writing — Python 2.7.12rc1 documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just be sure you have:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1. No ASCII Null Data&lt;/P&gt;&lt;P&gt;2. UTF-8 Format or ASCII printable&amp;nbsp; delimiters&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import csv
reader = csv.DictReader(open('somefile.txt', 'rb'), &lt;STRONG&gt;delimiter='\t'&lt;/STRONG&gt;)
for row in reader:
&amp;nbsp; #do something &lt;EM&gt;more&lt;/EM&gt; useful here
&amp;nbsp; print row.get('new')&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:54:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22458#M1732</guid>
      <dc:creator>TedKowal</dc:creator>
      <dc:date>2021-12-10T20:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing text file and output to text file or csv</title>
      <link>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22459#M1733</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan Petterson, Rebecca Strauch and Ted Kowal for your inputs. Appreciated. It is solved now.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Jun 2016 20:22:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/parsing-text-file-and-output-to-text-file-or-csv/m-p/22459#M1733</guid>
      <dc:creator>KushendraShah1</dc:creator>
      <dc:date>2016-06-20T20:22:19Z</dc:date>
    </item>
  </channel>
</rss>

