<?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: Python Index Out of Range Error in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216240#M7418</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Can you post your script using CODE tags instead of PRE. If you're not sure how to, read this: &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code"&gt;How to post Python code&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 24 Apr 2013 01:50:45 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2013-04-24T01:50:45Z</dc:date>
    <item>
      <title>Python Index Out of Range Error</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216239#M7417</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;I am looping through a csv file using Python to remove header lines, unnecessary columns and trying to replace any blank latitude and longitude coordinate values with a value of (0, 0), these coordinates are in two separate columns. For this replace process I am using a ???for??? loop and the ???replace??? method and am indexing (in the case of the csv file I am working with, the index of the two columns are 36 and 37). However, I keep getting a ???list index out of range??? error message when doing this. Any thoughts to point me in a better direction?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
count = 0
for n in range(6):
&amp;nbsp;&amp;nbsp;&amp;nbsp; count = count + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; line = inFile.readline()

while line:
&amp;nbsp;&amp;nbsp;&amp;nbsp; count = count + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp; line = inFile.readline()
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Row', count, 'line info=', line[:72]
&amp;nbsp;&amp;nbsp;&amp;nbsp; lineList = line.split(',')
&amp;nbsp;&amp;nbsp;&amp;nbsp; newList = lineList[:72]
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'line info =', newList&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in newList[36]: #-&amp;gt; use "if" statement; for column, if name == "LatitudeNAD83" and 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; item.replace("", "0")
&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in newList[37]:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; item.replace("", "0")
&amp;nbsp;&amp;nbsp;&amp;nbsp; newLine = ','.join(newList)
&amp;nbsp;&amp;nbsp;&amp;nbsp; newLine = newLine + '\n'
&amp;nbsp;&amp;nbsp;&amp;nbsp; formatLine = newLine.replace("/","_")
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFile.write(formatLine) 
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;UPDATE:&lt;/STRONG&gt;&lt;SPAN&gt; Here's my code in correct format&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;newList is a list of field names, there should be 72 items in it.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 23 Apr 2013 15:26:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216239#M7417</guid>
      <dc:creator>MikeVann</dc:creator>
      <dc:date>2013-04-23T15:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: Python Index Out of Range Error</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216240#M7418</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Can you post your script using CODE tags instead of PRE. If you're not sure how to, read this: &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code"&gt;How to post Python code&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 24 Apr 2013 01:50:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216240#M7418</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2013-04-24T01:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Python Index Out of Range Error</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216241#M7419</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; I keep getting a �??list index out of range�?� error message when doing this. &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How many fields are there in the line? If you only have 10, lineList[36] will raise an out of range error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's another shot at this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# skip 5 rows
skipRows = 5
for n in range(skipRows + 1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; line = inFile.readline()
row = skipRows + 1
# read and clean up data
while line:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Row', row, 'line info=', line[:72]
&amp;nbsp;&amp;nbsp;&amp;nbsp; # clean line and split into list
&amp;nbsp;&amp;nbsp;&amp;nbsp; lineList = [f.strip().replace("/","_") for f in line.split(',')]
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'line info =', lineList
&amp;nbsp;&amp;nbsp;&amp;nbsp; latidx,lonidx = 36, 37 # lat and long are in field 36 and 37
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(lineList) &amp;gt; lonidx: 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise Exception, "line {0} too short".format(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for k in latidx, lonidx:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lineList&lt;K&gt; == "":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lineList&lt;K&gt; = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFile.write(','.join(newList) + "\n") 
&amp;nbsp;&amp;nbsp;&amp;nbsp; line = inFile.readline()
&amp;nbsp;&amp;nbsp;&amp;nbsp; row += 1&amp;nbsp;&amp;nbsp; 
&lt;/K&gt;&lt;/K&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:35:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216241#M7419</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T10:35:27Z</dc:date>
    </item>
    <item>
      <title>Re: Python Index Out of Range Error</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216242#M7420</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;newList is a list of field names, there should be 72 items in it&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Apparently not, if you're getting that error. I suggest using a try-except block to find which line is causing the problem (see my code for an example).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 25 Apr 2013 16:16:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-index-out-of-range-error/m-p/216242#M7420</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2013-04-25T16:16:30Z</dc:date>
    </item>
  </channel>
</rss>

