Function to create a dictionary returns empty dictionary when called

Hello! I’m beginner with Python (I’ve completed only 38 % of “Python for Everybody” course), and english isn’t my native tongue (I’ll try to be clear!).
I’m trying to code a program to read .txt files of shopping receipts in order to analyze them and draw conclusions. To begin with that, I thought it was necessary to create a dictionary with items and its prices.
I wrote a function that receives a list with open files as parameter, and returns the dictionary:

def supermarket_products(open_files):  # open_files is a list containing open .txt files of supermarket receipts.
    items = []
    prices = []
    for file in open_files:
        for line in file:
            line = line.rstrip()
            # In each receipt, the lines containing items don't include $ or the spanish word OFERTA.
            if "$" not in line and "OFERTA" not in line:
                word_list = line.split()
                # In a line, the first "word" is a numerical code, and the last one is the price.
                # The words in between are the item's name.
                item_words_list = word_list[1:len(word_list) - 1]
                item = ""
                for word in item_words_list:  # Turning words from the list to a single string.
                    item = item + " " + word
                items.append(item.lstrip())  # Adding item to list.
                # Now, extracting the price.
                price = word_list[-1]
                price = int(price.replace(".", ""))  # In receipts, prices contain a dot.
                prices.append(price)
    dictionary = dict(zip(items, prices))  # Using the lists to create a dictionary.
    return dictionary

Then, when I call the function, it returns an empty dictionary ({}). Why is this happening, and how can I fix it? I’m sorry if I made any typing or syntax mistake. Thank you in advance for your help!

No, I’m afraid I don’t have a Replit account. Instead, I’m using PyCharm Community on my desktop.

I had them in the internal storage of my computer. But I just added an example file on GitHub (I’m also a beginner there):

Thanks very much for replying so far. I’ll put the files I’ve written until now in that repository as soon as possible. If any new, I will announce it here in this topic. Thanks again!

Hello again! The files are already in the repository.

When executing the “program” code I get the error message: “IndexError: list index out of range”. It seems that the “word_list” in the “functions” code is empty, because the error occurs when I try to access the last index. Perhaps this also explains why the dictionary is finally empty. What am I doing wrong?

You are getting that error, because you are splitting line 11 and word_list is an empty list so it does not have any items/indices.

You can make a slight modification to the following line and skip empty lines all together.

if "$" not in line and "OFERTA" not in line:

Excellent! I made that modification and now the code works as I expected. Thank you very much for your help and time.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.