hmw1001
hmw1001

Reputation: 31

python split function does not work as expected

I am having a hard time to formulate what my problem is... It concerns this piece of code here:

def txt_to_dict_ad():

    my_dict = {}    

    with open('database.txt', 'r') as file:
        for line in file:
            temp = list(line.strip().split('-'))
            my_dict[temp[1].strip('\n')] = temp[0]

    return my_dict

When I run it and for example want to print the output of this function, I am getting an "index out of range" error for the line containing temp[1] and temp[0]. Why is that? And how can I avoid it?

The txt file contains Arabic and German vocabulary, Example data: Auto - سيارة

Upvotes: 1

Views: 1904

Answers (1)

Heiko Oberdiek
Heiko Oberdiek

Reputation: 1708

If a line in database.txt does not contain a -, then the variable temp contains a list with one element only and temp[1] of the next line tries to access the non-existent second element and will therefore throw the error.

You can avoid the error by ignoring lines without -, for example.

if '-' in line:
    temp = list(line.strip().split('-'))
    my_dict[temp[1].strip('\n')] = temp[0]

If you want to identify the lines without hyhen:

with open('database.txt', 'r') as file:
    for i, line in enumerate(file, start=1):
        if '-' in line:
            temp = list(line.strip().split('-'))
            my_dict[temp[1].strip('\n')] = temp[0]
        else:
            print('Line {} misses the underscore.'.format(i))

Upvotes: 1

Related Questions