Python Forum
IndexError: list index out of range - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: IndexError: list index out of range (/thread-34058.html)



IndexError: list index out of range - Laplace12 - Jun-22-2021

Working with a data file to simplify it - my aim is to get the stuff in the file in columns, and I've started with splitting and printing only the necessary lines, I'll still have to sort them out into columns somehow. Now the problem is that I'm trying to select all lines with 'Std deviations' in the data file, but since two of them have 3 values and the one only has one value, I get IndexError since on the last list there's no 4th or 5th value to print. Of course just putting in c[3] gives me the first value of all columns and no error, but then I'm losing four values.

output = '.txt'
with open(output) as file:
    for line in file:
        if '2101' in line and found:
            a = line.split()
            print(a[1])
        elif 'Lifetimes' in line and found:
            b = line.split()
            print(b[3], b[4], b[5])
        elif 'Std deviations' in line and found:
            c = line.split()
            print(c[3], c[4], c[5])
        elif 'Intensities' in line and found:
            d = line.split()
            print(d[3], d[4], d[5])
        elif 'Time-zero' in line and found:
            e = line.split()
            print(e[4])
        else:
            found = True
The error is:

runfile('C:/.../output.py', wdir='C:/.../Python scripts')
#0
0.4000 0.1250 2.0446
Fixed Fixed 0.0339
69.2721 9.6726 21.0553
1.0359 0.8128 0.4063
41.5603
Traceback (most recent call last):

  File "<ipython-input-128-f8159069ae40>", line 1, in <module>
    runfile('C:/.../output.py', wdir='C:/.../Python scripts')

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\...\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/.../output.py", line 47, in <module>
    print(c[3], c[4], c[5])

IndexError: list index out of range
For reference, the 'output' file looks like this:

0-70 #0                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0446
             Std deviations   :     Fixed     Fixed    0.0339 
             Intensities (%)  :   69.2721    9.6726   21.0553
             Std deviations   :    1.0359    0.8128    0.4063 
Time-zero    Channel number   :   41.5603
             Std deviations   :    0.0588 
0-70 #1                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0714
             Std deviations   :     Fixed     Fixed    0.0344 
             Intensities (%)  :   70.0338    9.0952   20.8710
             Std deviations   :    1.0308    0.8135    0.4009 
Time-zero    Channel number   :   41.5853
             Std deviations   :    0.0593 
0-70 #2                                    
             Lifetimes (ns)   :    0.4000    0.1250    2.0568
             Std deviations   :     Fixed     Fixed    0.0333 
             Intensities (%)  :   69.5963    8.7445   21.6592
             Std deviations   :    1.0411    0.8177    0.4072 
Time-zero    Channel number   :   41.5541
             Std deviations   :    0.0603 
and so on for up to #100. My aim is to sort the whole thing into a text file with columns for dataset number, lifetimes, deviations, intensities, deviations, time-zero and deviation, and I figured using split to get the specific lines out of the data would be the best thing to start with. Is there a way to assign a command for the time-zero deviation even though 'Std deviations' appears in the same way for everything in the file? Basically I just want this to print, for example for the first set, the 0.0588 value as well.


RE: IndexError: list index out of range - Yoriz - Jun-22-2021

This will work with 1,2 or 3 deviation values and gives a default empty string for cases where there is no value.
You can change the default if you want something else by changing the else value.
line3deviations = '             Std deviations   :    1.0308    0.8135    0.4009 '
line2deviations = '             Std deviations   :    0.0308    1.2135 '
line1deviations = '             Std deviations   :    0.0593 '
line0deviations = '             Std deviations   : '


def get_deviations(line):
    split_line = line.split()
    return [split_line[num] if split_line[num:] else '' for num in range(3, 6)]


print(get_deviations(line3deviations))
print(get_deviations(line2deviations))
print(get_deviations(line1deviations))
print(get_deviations(line0deviations))
Output:
['1.0308', '0.8135', '0.4009'] ['0.0308', '1.2135', ''] ['0.0593', '', ''] ['', '', '']