IndexError: list index out of range python
<p>The follwoing code takes a log file and re-writes it in csv format: </p>

<pre><code>file = open("C:\\Scripts\\logs\\SteelUsage2.bsu")
DataFile = open("C:\\Scripts\\MFGData90.csv","a+")
for line in file.readlines():   
    #print line
    line=line.strip()
    numbers=line.split()
    #print numbers
    #print numbers[0]
    #alpha = numbers [0]
    #print alpha
    StartHour= numbers[0]
    StartMinute= numbers[1]
    StartDay=numbers[2]
    StartMonth=numbers[3]
    StartYear=numbers[4]
    EndHour=numbers[5]
    EndMinute=numbers[6]
    Meters=numbers[10]
    #float(Meters)
    #round(Meters)
    #int(Meters)
    #print "before if "+Meters
    if Meters &gt; "0.1":
        #print "after if "+Meters
        print StartDay+"/"+StartMonth+"/"+StartYear+","+StartMinute+":"+StartHour+","+EndMinute+":"+EndHour+","+Meters+"\n"
        #DataFile.write (StartDay+"/"+StartMonth+"/"+StartYear+","+"90"+","+Meters+","+StartMinute+":"+StartHour+","+EndMinute+":"+EndHour+"\n") 

DataFile.close()
file.close()  
</code></pre>

<p>example log file: </p>

<pre><code> 49 12 5 9 2016 14 15 5 9 2016 219.682
 14 15 5 9 2016 15 15 5 9 2016 0
 12 11 6 9 2016 45 11 6 9 2016 83.691
 46 11 6 9 2016 56 11 6 9 2016 41.125
 6 11 7 9 2016 7 11 7 9 2016 0
 7 11 7 9 2016 8 11 7 9 2016 0
 32 12 7 9 2016 32 12 7 9 2016 0
 33 12 7 9 2016 54 12 7 9 2016 50.823
</code></pre>

<p>example output: </p>

<pre><code>5/9/2016,12:49,15:14,219.682

6/9/2016,11:12,11:45,83.691

6/9/2016,11:46,11:56,41.125

7/9/2016,12:33,12:54,50.823  
</code></pre>

<p>but when the log file contains non integer characters like this: </p>

<pre><code> 49 12 5 9 2016 14 15 5 9 2016 219.682
 NAME
 14 15 5 9 2016 15 15 5 9 2016 0
 12 11 6 9 2016 45 11 6 9 2016 83.691
 46 11 6 9 2016 56 11 6 9 2016 41.125
 6 11 7 9 2016 7 11 7 9 2016 0
 7 11 7 9 2016 8 11 7 9 2016 0
 32 12 7 9 2016 32 12 7 9 2016 0
 33 12 7 9 2016 54 12 7 9 2016 50.823  
</code></pre>

<p>iget the follwing error:</p>

<pre><code>Traceback (most recent call last):
  File "C:\Scripts\test.py", line 12, in &lt;module&gt;
    StartMinute= numbers[1]
IndexError: list index out of range  
</code></pre>

<p>where did i go wrong?
thanks!</p>

<p>You take line after line of your log and split is in separate parts. (11 in your case).</p>

<p>So if you want to access a part of the line which doesn't exist, you get an error. 
Try checking every line if it has all parts and access then.</p>

<p>I wouldn't suggest using continue, because its considered as bad practice.
It's better to explicitly do your stuff only when the number of elements is right.</p>

<pre><code>numbers = line.split()
if len(numbers) &gt;= 11:
  // Do your stuff
</code></pre>

python