jeudi 8 février 2018

IndexError when accessing list elements

I am trying to extract values from a list (saved in the file 'Verhaltens_Baum_Read'). See below its first 4 lines:

['Possibilities', 'Condition', 'Gen', 'Insight', 'False', 'Solved', 'OldNew', 'RemKnow']  
['1', 'Exp', 'Erkannt', 'AHA', 'Real', 'Yes 1', 'New', 'Ka']  
['2', 'Base', 'Erkannt', 'AHA', 'Real', 'Yes 1', 'New', 'Ka']  
['3', 'Exp', 'Erkannt', 'AHA', 'Real', 'Yes 1', 'Old', 'Remember']

For so, I wrote this code:

Condition_Names = open('Condition_Names.txt','w')

file = open('Verhaltens_Baum_Read.txt','r')

for splitted in file:
    if splitted[1] == 'Exp':
        Condition_Names.write('Exp_')
    if splitted[1] == 'Base':
        Condition_Names.write('Base_')
    if splitted[2] == 'Erkannt':
        Condition_Names.write('Gen_')
    if splitted[2] == 'Nicht Erkannt':
        Condition_Names.write('NotGen_')    
    if splitted[3] == 'AHA':
        Condition_Names.write('Aha_')
    if splitted[3] == 'Kein AHA':
        Condition_Names.write('NoAha_')
    if splitted[4] == ('Real'):
        Condition_Names.write('Real_')
    if splitted[4] == ('False'):
        Condition_Names.write('False_')
    if splitted[5] == ('Yes 1'):          #This is the line 27
        Condition_Names.write('Solved_')
    if splitted[5] == ('No 0'):
        Condition_Names.write('NotSolved_')
    if splitted[6] == 'Old':
        Condition_Names.write('Old_')
    if splitted[6] == 'New':
        Condition_Names.write('New_')
    if splitted[7] == 'Ka':
        Condition_Names.write('Ka\n')
    if splitted[7] == 'Remember':
        Condition_Names.write('Rem\n')
    if splitted[7] == 'Recognized':
        Condition_Names.write('Recog\n')

and then I want my code to write names in another text file according to the occurrences on the list. The output to my code is:

Traceback (most recent call last):
  File "./Condition_Names_Generate.py", line 27, in <module>
    if splitted[5] == ('Yes 1'):
IndexError: string index out of range

I really cannot understand why it happens. The splitted[5] item was saved as a string on the list, as you can see it above. I would appreciate any help.

Aucun commentaire:

Enregistrer un commentaire