Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#1
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.
Reply
#2
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', '', ''] ['', '', '']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: list index out of range dolac 4 404 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 604 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 584 May-03-2022, 01:39 PM
Last Post: Anldra12
  TypeError: list indices must be integers or slices, not range Anldra12 2 717 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 1,367 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 1,441 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 1,117 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  IndexError: list index out of range brunolelli 11 3,604 Mar-25-2021, 11:36 PM
Last Post: brunolelli
  index of range, but data prints out mrc06405j 1 1,371 Mar-25-2021, 07:20 PM
Last Post: buran
  Changing Index of 2 List in python giddyhead 0 1,061 Mar-05-2021, 05:45 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020